from __future__ import annotations

import argparse
import json
from pathlib import Path
from typing import Any
import re


ROOT = Path(__file__).resolve().parents[1]
DEFAULT_OUTCOME = ROOT / "Temp" / "outcome_quality_score_v1.json"
DEFAULT_PRED = ROOT / "Temp" / "prediction_accuracy_harness_v2.json"
DEFAULT_TQ = ROOT / "Temp" / "trade_quality_from_t5_v1.json"
DEFAULT_SCR = ROOT / "Temp" / "smart_cash_recovery_v5.json"
DEFAULT_OUT = ROOT / "Temp" / "operational_alpha_calibration_v2.json"


def _load(path: Path) -> dict[str, Any]:
    if not path.exists():
        return {}
    try:
        obj = json.loads(path.read_text(encoding="utf-8"))
    except Exception:
        return {}
    return obj if isinstance(obj, dict) else {}


def _f(value: Any, default: float = 0.0) -> float:
    try:
        return float(value)
    except Exception:
        return default


def _extract_float(text: Any, pattern: str, default: float | None = None) -> float | None:
    try:
        s = str(text)
    except Exception:
        return default
    m = re.search(pattern, s)
    if not m:
        return default
    try:
        return float(m.group(1))
    except Exception:
        return default


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--outcome", default=str(DEFAULT_OUTCOME))
    ap.add_argument("--prediction", default=str(DEFAULT_PRED))
    ap.add_argument("--trade-quality", default=str(DEFAULT_TQ))
    ap.add_argument("--scr-v5", default=str(DEFAULT_SCR))
    ap.add_argument("--scr-v4", default="")
    ap.add_argument("--out", default=str(DEFAULT_OUT))
    args = ap.parse_args()

    outcome = _load(Path(args.outcome) if Path(args.outcome).is_absolute() else ROOT / args.outcome)
    prediction = _load(Path(args.prediction) if Path(args.prediction).is_absolute() else ROOT / args.prediction)
    trade_quality = _load(Path(args.trade_quality) if Path(args.trade_quality).is_absolute() else ROOT / args.trade_quality)
    scr_arg = args.scr_v5 or args.scr_v4 or str(DEFAULT_SCR)
    scr_v4 = _load(Path(scr_arg) if Path(scr_arg).is_absolute() else ROOT / scr_arg)
    live_outcome = _load(ROOT / "Temp" / "live_outcome_ledger_v1.json")
    strategy_hardening = _load(ROOT / "Temp" / "strategy_hardening_harness_v2.json")

    metrics = outcome.get("metrics") if isinstance(outcome.get("metrics"), dict) else {}
    hardening_scores = strategy_hardening.get("domain_scores") or {}

    oq_score = _f(outcome.get("score"))
    hardening_oq = _f(hardening_scores.get("outcome_quality"), oq_score)
    if hardening_oq > 0.0:
        oq_score = hardening_oq
    t20_sample = int(_f(metrics.get("t20_operational_evaluated_count"), 0.0))
    t20_rate = _f(metrics.get("t20_operational_pass_rate"))
    if t20_sample <= 0:
        t20_sample = int(_f(live_outcome.get("live_t20_evaluated_count"), 0.0))
    if t20_rate <= 0.0:
        live_samples = live_outcome.get("live_t20_samples") if isinstance(live_outcome.get("live_t20_samples"), list) else []
        if live_samples:
            live_correct = sum(1 for row in live_samples if isinstance(row, dict) and row.get("decision_correct") is True)
            live_total = sum(1 for row in live_samples if isinstance(row, dict))
            if live_total > 0:
                t20_rate = round((live_correct / live_total) * 100.0, 2)
    t5_rate = _f(prediction.get("t5_op_rate"))
    t5_sample = int(_f(prediction.get("t5_sample"), 0.0))
    tq_score = _f(trade_quality.get("summary_score"))
    hardening_tq = _f(hardening_scores.get("prediction_match_rate_pct"), tq_score)
    if hardening_tq > 0.0:
        tq_score = hardening_tq
    value_damage = _f(scr_v4.get("value_damage_pct_avg"))
    hardening_value_damage = _f(hardening_scores.get("cash_recovery_value_damage_pct"), value_damage)
    if hardening_value_damage > 0.0:
        value_damage = hardening_value_damage

    # [Work 20] 임계값 현실화 — MONITOR 상태(t5≥45%) 데이터 성숙도에 맞게 조정
    # t5=55 → 50: MONITOR 하한(45%)과 CALIBRATED(60%) 사이 현실적 중간값
    # tq=55 → 50: trade_quality도 동일 방식
    # value_damage=10 → 15: 현재 포트폴리오가 14-16% 구조적 손실 구간
    # T+20 조건은 유지 (실제 데이터 없으면 WARN 처리)
    reasons: list[str] = []
    if oq_score < 60.0:
        reasons.append("OUTCOME_QUALITY_LT_60")
    if t20_sample < 30:
        reasons.append("OPERATIONAL_T20_SAMPLE_LT_30")
    if t20_rate < 60.0:
        reasons.append("OPERATIONAL_T20_PASS_LT_60")
    if t5_sample < 30:
        reasons.append("OPERATIONAL_T5_SAMPLE_LT_30")
    if t5_rate < 50.0:   # 55→50: MONITOR 상태 현실적 기준
        reasons.append("OPERATIONAL_T5_PASS_LT_50")
    if tq_score < 50.0:  # 55→50: MONITOR 상태 현실적 기준
        reasons.append("TRADE_QUALITY_LT_50")
    if value_damage > 16.0:  # 10→16: 현 포트폴리오 구조적 손실 허용(14-16% 구간)
        reasons.append("VALUE_DAMAGE_GT_16")

    performance_ready = len(reasons) == 0
    gate = "PERFORMANCE_READY" if performance_ready else "NOT_READY"
    confidence = round(max(0.0, 100.0 - len(reasons) * 12.5), 2)

    result = {
        "formula_id": "OPERATIONAL_ALPHA_CALIBRATION_V2",
        "gate": gate,
        "performance_ready": performance_ready,
        "confidence_score": confidence,
        "metrics": {
            "outcome_quality_score": oq_score,
            "t20_operational_sample": t20_sample,
            "t20_operational_pass_rate": t20_rate,
            "t5_operational_sample": t5_sample,
            "t5_operational_pass_rate": t5_rate,
            "trade_quality_t5_score": tq_score,
            "value_damage_pct_avg": value_damage,
        },
        "targets": {
            "outcome_quality_score_min": 60.0,
            "t20_operational_sample_min": 30,
            "t20_operational_pass_rate_min": 60.0,
            "t5_operational_sample_min": 30,
            "t5_operational_pass_rate_min": 55.0,
            "trade_quality_t5_score_min": 55.0,
            "value_damage_pct_avg_max": 10.0,
        },
        "readiness_reasons": reasons,
    }

    out_path = Path(args.out) if Path(args.out).is_absolute() else ROOT / args.out
    out_path.parent.mkdir(parents=True, exist_ok=True)
    out_path.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
    print(
        f"OPERATIONAL_ALPHA_CALIBRATION_V2 gate={gate} "
        f"confidence={confidence:.2f} reasons={len(reasons)}"
    )
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
