#!/usr/bin/env python3
"""
🔍 過濾特定執行動作
"""
import json
import sys
from datetime import datetime

def filter_actions(action_type=None):
    """過濾並顯示特定類型的動作"""
    
    try:
        with open('actions.json', 'r') as f:
            actions = json.load(f)
    except:
        print("❌ 找不到動作記錄")
        return
    
    # 過濾動作類型
    if action_type:
        filtered = [a for a in actions if action_type.upper() in a['type'].upper()]
        print(f"\n🔍 篩選: {action_type} ({len(filtered)} 個結果)")
    else:
        filtered = actions
        print(f"\n📝 所有動作 ({len(filtered)} 個)")
    
    print("=" * 60)
    
    # 顯示過濾結果
    for action in filtered[-10:]:  # 最近10個
        time = datetime.fromisoformat(action['timestamp'])
        print(f"\n[{time.strftime('%m-%d %H:%M')}] {action['type']}")
        print(f"  → {action['details']}")
        
        # 如果是交易，顯示特殊標記
        if 'TRADE' in action['type']:
            print(f"  💰 重要交易動作")
        elif 'RISK' in action['type']:
            print(f"  ⚠️ 風險事件")
        elif 'PROFIT' in action['type']:
            print(f"  ✅ 獲利動作")

if __name__ == "__main__":
    # 從命令行參數獲取過濾條件
    filter_type = sys.argv[1] if len(sys.argv) > 1 else None
    filter_actions(filter_type)