"""
DeepCoin 프로젝트 경로 (data + docs 통합).

docs/
  reference/     가이드·기법 명세 (Git 추적)
  02_ground_truth/ … 05_ops/, charts/  단계별 산출물 (로컬 재생성, Git 제외)
"""

from __future__ import annotations

import os
from pathlib import Path

# DeepCoin/ (이 파일: DeepCoin/deepcoin/paths.py)
PROJECT_ROOT = Path(__file__).resolve().parents[1]

# --- data ---
DATA_DIR = PROJECT_ROOT / "data"
DB_DIR = DATA_DIR
GROUND_TRUTH_DIR = DATA_DIR / "ground_truth"
OPS_STATE_DIR = DATA_DIR / "ops"
COOLDOWN_FILE = OPS_STATE_DIR / "coins_buy_time.json"

# --- docs (reference + 단계별 산출물) ---
DOCS_DIR = PROJECT_ROOT / "docs"
DOCS_REFERENCE_DIR = DOCS_DIR / "reference"

DOCS_CHARTS = DOCS_DIR / "charts"
DOCS_GROUND_TRUTH = DOCS_DIR / "02_ground_truth"
DOCS_ANALYSIS = DOCS_DIR / "03_analysis"
DOCS_MATCHING = DOCS_DIR / "04_matching"
DOCS_OPS = DOCS_DIR / "05_ops"

ANALYSIS_TRADES_CSV = DOCS_ANALYSIS / "general_analysis_trades.csv"
ANALYSIS_GT_MTF_PROFILE_JSON = DOCS_ANALYSIS / "gt_mtf_profile.json"
ANALYSIS_GT_MTF_PROFILE_HTML = DOCS_ANALYSIS / "gt_mtf_profile_report.html"
ANALYSIS_GT_CALIBRATION_JSON = DOCS_ANALYSIS / "gt_calibration_report.json"
ANALYSIS_REPORT_HTML = DOCS_ANALYSIS / "general_analysis_report.html"
ANALYSIS_CAPABILITY_HTML = DOCS_ANALYSIS / "general_analysis_capability.html"
ANALYSIS_LATEST_DIR = DOCS_ANALYSIS / "latest"

MATCHING_RULE_CANDIDATES = DOCS_MATCHING / "rule_candidates.json"
MATCHING_RULE_FIRES = DOCS_MATCHING / "rule_fires.csv"
MATCHING_FIRE_OUTCOMES = DOCS_MATCHING / "fire_outcomes.csv"
MATCHING_MATCHED_RULES = DOCS_MATCHING / "matched_rules.json"
MATCHING_BACKTEST_HTML = DOCS_MATCHING / "backtest_summary.html"
MATCHING_GT_OVERLAP = DOCS_MATCHING / "gt_overlap_report.json"
MATCHING_SIMULATION_JSON = DOCS_MATCHING / "simulation_report.json"
MATCHING_SIMULATION_HTML = DOCS_MATCHING / "simulation_report.html"
MATCHING_CAUSAL_GT_CALIBRATION_JSON = DOCS_MATCHING / "causal_gt_calibration.json"
MATCHING_HYBRID_DD_CALIBRATION_JSON = DOCS_MATCHING / "hybrid_dd_calibration.json"
MATCHING_GT_COMPARISON_JSON = DOCS_MATCHING / "gt_comparison_report.json"
MATCHING_GT_COMPARISON_HTML = DOCS_MATCHING / "gt_comparison_report.html"

LIVE_TRADES_LOG = OPS_STATE_DIR / "live_trades.jsonl"
PAPER_FIRES_LOG = OPS_STATE_DIR / "paper_fires.jsonl"
PAPER_PORTFOLIO_JSON = OPS_STATE_DIR / "paper_portfolio.json"
LIVE_SIGNAL_HISTORY_JSON = OPS_STATE_DIR / "live_signal_history.json"
PAPER_WEEKLY_REPORT_JSON = DOCS_OPS / "phase_c_paper_report.json"
PHASE_C_DAILY_DIR = DOCS_OPS / "phase_c_daily"
PHASE_C_SUPERVISOR_LOG = OPS_STATE_DIR / "phase_c_supervisor.log"
PHASE_C_SUPERVISOR_PID = OPS_STATE_DIR / "phase_c_supervisor.pid"

CHART_BB_HTML = DOCS_CHARTS / "wld_bb_chart.html"
CHART_TRUTH_HTML = DOCS_GROUND_TRUTH / "wld_ground_truth_chart.html"

# 하위 호환 (구 reports/ 이름)
REPORTS_DIR = DOCS_DIR
REPORTS_CHARTS = DOCS_CHARTS
REPORTS_GROUND_TRUTH = DOCS_GROUND_TRUTH
REPORTS_ANALYSIS = DOCS_ANALYSIS
REPORTS_MATCHING = DOCS_MATCHING
REPORTS_OPS = DOCS_OPS


def resolve_db_path() -> Path:
    """존재하는 coins.db 경로."""
    candidates = [
        DATA_DIR / "coins.db",
        PROJECT_ROOT / "coins.db",
    ]
    for p in candidates:
        if p.is_file():
            return p
    return DATA_DIR / "coins.db"


def resolve_ground_truth_file() -> Path:
    """존재하는 ground_truth_trades.json 경로."""
    name = os.getenv("GROUND_TRUTH_FILE", "ground_truth_trades.json")
    p = Path(name)
    if p.is_absolute():
        return p
    candidates = [
        GROUND_TRUTH_DIR / "ground_truth_trades.json",
        PROJECT_ROOT / "ground_truth_trades.json",
        PROJECT_ROOT / name,
    ]
    for c in candidates:
        if c.is_file():
            return c
    return GROUND_TRUTH_DIR / "ground_truth_trades.json"


def ensure_dirs() -> None:
    """단계별 출력·가이드 디렉터리 생성."""
    for d in (
        DATA_DIR,
        GROUND_TRUTH_DIR,
        OPS_STATE_DIR,
        DOCS_DIR,
        DOCS_REFERENCE_DIR,
        DOCS_CHARTS,
        DOCS_GROUND_TRUTH,
        DOCS_ANALYSIS,
        DOCS_MATCHING,
        DOCS_OPS,
        ANALYSIS_LATEST_DIR,
    ):
        d.mkdir(parents=True, exist_ok=True)
