"""
general_analysis 실행 진입점.

  python scripts/03_analyze_trades.py
  python scripts/03_analyze_trades.py --limit 20   # 테스트용 타점 수 제한
"""

from __future__ import annotations

import argparse
import json
from pathlib import Path

from config import CHART_LOOKBACK_DAYS, SYMBOL
from deepcoin.analysis.general_analysis_config import (
    DEFAULT_OUTPUT_CSV,
    DEFAULT_OUTPUT_HTML,
    DEFAULT_TRADES_FILE,
)
from deepcoin.analysis.general_analysis_report import write_analysis_report
from deepcoin.analysis.general_analysis_snapshot import export_trade_snapshots
from deepcoin.ops.monitor import Monitor
from deepcoin.data.mtf_bb import load_frames_from_db


def main() -> None:
    """ground truth 타점 MTF general_analysis 스냅샷 생성."""
    parser = argparse.ArgumentParser(description="general_analysis MTF 타점 분석")
    parser.add_argument("--limit", type=int, default=0, help="타점 수 제한 (0=전체)")
    parser.add_argument("--trades", type=str, default=DEFAULT_TRADES_FILE)
    parser.add_argument("--csv", type=str, default=DEFAULT_OUTPUT_CSV)
    parser.add_argument("--html", type=str, default=DEFAULT_OUTPUT_HTML)
    args = parser.parse_args()

    from deepcoin.paths import REPORTS_ANALYSIS

    trades_path = Path(args.trades)
    data = json.loads(trades_path.read_text(encoding="utf-8"))
    trades = data.get("trades") or []
    if args.limit > 0:
        trades = trades[: args.limit]
        print(f"테스트 모드: 타점 {args.limit}건만")

    import sys
    import time

    print(f"=== 03b GT 타점 MTF 분석 {SYMBOL} (lookback {CHART_LOOKBACK_DAYS}일) ===")
    print(f"  간격: 3,5,10,15,30,60,240,1440분 (1분 제외) · 타점 {len(trades)}건")
    sys.stdout.flush()
    t0 = time.time()
    print("[03b] Phase 0: DB에서 8TF OHLCV 로드...")
    sys.stdout.flush()
    mon = Monitor(cooldown_file=None)
    frames = load_frames_from_db(mon, SYMBOL, lookback_days=CHART_LOOKBACK_DAYS)
    if not frames:
        raise RuntimeError("coins.db 데이터 없음")
    for iv in (3, 5, 10, 15, 30, 60, 240, 1440):
        df = frames.get(iv)
        n = len(df) if df is not None else 0
        print(f"  WLD_{iv}: {n:,}봉")
    print(f"[03b] Phase 0 완료 ({time.time() - t0:.0f}초)")
    sys.stdout.flush()

    # limit 시 임시 trades 파일
    if args.limit > 0:
        tmp = REPORTS_ANALYSIS / "_ga_trades_subset.json"
        tmp.parent.mkdir(exist_ok=True)
        subset = {**data, "trades": trades}
        tmp.write_text(json.dumps(subset, ensure_ascii=False), encoding="utf-8")
        trades_path = tmp

    from deepcoin.analysis.general_analysis_snapshot import build_trade_mtf_snapshots

    csv_path = Path(args.csv)
    df = build_trade_mtf_snapshots(frames, trades)
    csv_path.parent.mkdir(parents=True, exist_ok=True)
    df.to_csv(csv_path, index=False, encoding="utf-8-sig")
    print(f"저장: {csv_path} ({len(df)}행 × {len(df.columns)}열)")

    write_analysis_report(csv_path, Path(args.html))

    from deepcoin.matching.gt_mtf_profile import run_gt_mtf_profile

    run_gt_mtf_profile(csv_path)
    print("완료.")


if __name__ == "__main__":
    main()
