#!/usr/bin/env python3
"""
將 OpenClaw 台股評分卡轉換為 GitHub 格式
"""
import yaml
import json
from datetime import datetime
import os

# 20 檔觀察名單
WATCHLIST = [
    # AI 伺服器
    ("2382", "廣達", "AI伺服器"),
    ("3231", "緯創", "AI伺服器"),
    ("6669", "緯穎", "AI伺服器"),
    ("2356", "英業達", "AI伺服器"),
    ("2308", "台達電", "AI伺服器"),
    
    # 半導體設備
    ("3680", "家登", "半導體設備"),
    ("6196", "帆宣", "半導體設備"),
    ("2404", "漢唐", "半導體設備"),
    ("6139", "亞翔", "半導體設備"),
    ("3413", "京鼎", "半導體設備"),
    
    # 記憶體
    ("2408", "南亞科", "記憶體"),
    ("2344", "華邦電", "記憶體"),
    ("8299", "群聯", "記憶體"),
    ("3260", "威剛", "記憶體"),
    ("2451", "創見", "記憶體"),
    
    # 能源
    ("1513", "中興電", "能源"),
    ("1503", "士電", "能源"),
    ("1514", "亞力", "能源"),
    ("2371", "大同", "能源"),
]

def create_stock_yaml(ticker, name, industry):
    """建立單檔股票的 YAML 格式"""
    return {
        'ticker': f"TW-{ticker}",
        'name': name,
        'industry': industry,
        'last_updated': datetime.now().strftime('%Y-%m-%d'),
        
        'scores': {
            'moat': 2,
            'management': 2, 
            'financial': 2,
            'reinvestment': 2,
            'valuation': 2,
            'risk': 2,
            'total': 12,
            'grade': 'B'
        },
        
        'reasoning': {
            'moat': '待評估',
            'management': '待評估',
            'financial': '待評估', 
            'reinvestment': '待評估',
            'valuation': '待評估',
            'risk': '待評估'
        },
        
        'thesis': {
            'how_money': '待分析',
            'sustainability': '待分析',
            'main_risk': '待分析'
        },
        
        'watch_points': [
            '關鍵指標1',
            '關鍵指標2'
        ],
        
        'changes': [
            {
                'date': datetime.now().strftime('%Y-%m-%d'),
                'item': 'initial',
                'delta': 0,
                'reason': '初始建檔'
            }
        ],
        
        'triggers': ['初始建檔']
    }

def export_all():
    """匯出所有股票到 GitHub 格式"""
    os.makedirs('watchlist', exist_ok=True)
    
    for ticker, name, industry in WATCHLIST:
        stock_data = create_stock_yaml(ticker, name, industry)
        filename = f"watchlist/TW-{ticker}-{name}.yml"
        
        with open(filename, 'w', encoding='utf-8') as f:
            yaml.dump(stock_data, f, allow_unicode=True, default_flow_style=False)
        
        print(f"✅ 建立 {filename}")

    # 建立 config 檔案
    config = {
        'system': {
            'name': 'Taiwan Stock Research System',
            'dikw_models': {
                'data': 'anthropic/claude-haiku-3-5-20241022',
                'information': 'anthropic/claude-sonnet-4-20250514', 
                'knowledge': 'anthropic/claude-sonnet-4-20250514',
                'wisdom': 'anthropic/claude-opus-4-6'
            },
            'industry_allocation': {
                'AI伺服器': 0.35,
                '半導體設備': 0.25,
                '能源': 0.25, 
                '記憶體': 0.15
            }
        }
    }
    
    with open('config.yml', 'w', encoding='utf-8') as f:
        yaml.dump(config, f, allow_unicode=True, default_flow_style=False)
    
    print("✅ 建立系統設定檔")
    print(f"總共匯出 {len(WATCHLIST)} 檔股票")

if __name__ == "__main__":
    export_all()