#!/usr/bin/env python3
"""
build_ui_state 각 단계 직접 테스트
"""

import sys
import os
sys.path.insert(0, os.getcwd())

# src.quant_engine 패키지를 임포트할 수 있도록
import importlib.util
spec = importlib.util.spec_from_file_location(
    "snapshot_admin_store_v1",
    "src/quant_engine/snapshot_admin_store_v1.py"
)
store = importlib.util.module_from_spec(spec)
sys.modules['snapshot_admin_store_v1'] = store
spec.loader.exec_module(store)

from pathlib import Path
from datetime import datetime
import time

db_path = Path('src/quant_engine/snapshot_admin.db')

print("="*80)
print("build_ui_state 각 단계 테스트")
print("="*80)

functions_to_test = [
    ('summarize_workspace', lambda: store.summarize_workspace(db_path)),
    ('load_settings_rows', lambda: store.load_settings_rows(db_path)),
    ('load_account_snapshot_rows', lambda: store.load_account_snapshot_rows(db_path)),
    ('validate_settings_rows', lambda: store.validate_settings_rows(store.load_settings_rows(db_path))),
    ('validate_account_snapshot_rows', lambda: store.validate_account_snapshot_rows(store.load_account_snapshot_rows(db_path))),
    ('load_approval_rows', lambda: store.load_approval_rows(db_path)),
    ('load_locks', lambda: store.load_locks(db_path)),
    ('load_change_log_rows', lambda: store.load_change_log_rows(db_path, limit=12)),
]

for name, func in functions_to_test:
    try:
        start = time.time()
        result = func()
        elapsed = time.time() - start
        if isinstance(result, list):
            print(f"[OK] {name}: {len(result)} 항목, {elapsed:.2f}s")
        elif isinstance(result, dict):
            print(f"[OK] {name}: {len(result)} 키, {elapsed:.2f}s")
        else:
            print(f"[OK] {name}: {type(result).__name__}, {elapsed:.2f}s")
    except Exception as e:
        print(f"[ERROR] {name}: {e}")
        import traceback
        traceback.print_exc()
        break

print("\n[완료]")
