#!/usr/bin/env python3
"""
build_ui_state 함수의 각 단계 테스트
"""

import sys
sys.path.insert(0, '.')

# Import from package
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)
spec.loader.exec_module(store)

from snapshot_admin_store_v1 import (
    summarize_workspace,
    load_settings_rows,
    load_account_snapshot_rows,
    validate_settings_rows,
    validate_account_snapshot_rows,
    load_approval_rows,
    load_locks,
    load_change_log_rows,
)
from pathlib import Path
import time

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

print("="*80)
print("build_ui_state 함수 단계별 테스트")
print("="*80)

# 1. summarize_workspace
print("\n[1] summarize_workspace")
try:
    start = time.time()
    result = summarize_workspace(db_path)
    elapsed = time.time() - start
    print(f"  [OK] {elapsed:.2f}s")
except Exception as e:
    print(f"  [ERROR] {e}")

# 2. load_settings_rows
print("\n[2] load_settings_rows")
try:
    start = time.time()
    result = load_settings_rows(db_path)
    elapsed = time.time() - start
    print(f"  [OK] {len(result)} rows, {elapsed:.2f}s")
except Exception as e:
    print(f"  [ERROR] {e}")

# 3. load_account_snapshot_rows
print("\n[3] load_account_snapshot_rows")
try:
    start = time.time()
    result = load_account_snapshot_rows(db_path)
    elapsed = time.time() - start
    print(f"  [OK] {len(result)} rows, {elapsed:.2f}s")
except Exception as e:
    print(f"  [ERROR] {e}")

# 4. validate_settings_rows
print("\n[4] validate_settings_rows")
try:
    start = time.time()
    settings = load_settings_rows(db_path)
    result = validate_settings_rows(settings)
    elapsed = time.time() - start
    print(f"  [OK] {len(result)} errors, {elapsed:.2f}s")
except Exception as e:
    print(f"  [ERROR] {e}")

# 5. validate_account_snapshot_rows
print("\n[5] validate_account_snapshot_rows")
try:
    start = time.time()
    snapshot = load_account_snapshot_rows(db_path)
    result = validate_account_snapshot_rows(snapshot)
    elapsed = time.time() - start
    print(f"  [OK] {len(result)} errors, {elapsed:.2f}s")
except Exception as e:
    print(f"  [ERROR] {e}")

# 6. load_approval_rows
print("\n[6] load_approval_rows")
try:
    start = time.time()
    result = load_approval_rows(db_path)
    elapsed = time.time() - start
    print(f"  [OK] {len(result)} rows, {elapsed:.2f}s")
except Exception as e:
    print(f"  [ERROR] {e}")

# 7. load_locks
print("\n[7] load_locks")
try:
    start = time.time()
    result = load_locks(db_path)
    elapsed = time.time() - start
    print(f"  [OK] {len(result)} rows, {elapsed:.2f}s")
except Exception as e:
    print(f"  [ERROR] {e}")

# 8. load_change_log_rows
print("\n[8] load_change_log_rows")
try:
    start = time.time()
    result = load_change_log_rows(db_path, limit=12)
    elapsed = time.time() - start
    print(f"  [OK] {len(result)} rows, {elapsed:.2f}s")
except Exception as e:
    print(f"  [ERROR] {e}")

# 9. Full build_ui_state (at the end)
print("\n[9] build_ui_state (FULL)")
try:
    start = time.time()
    result = build_ui_state(db_path)
    elapsed = time.time() - start
    print(f"  [OK] keys={len(result)}, {elapsed:.2f}s")
except Exception as e:
    print(f"  [ERROR] {e}")
    import traceback
    traceback.print_exc()

print("\n[완료]")
