from __future__ import annotations

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


ROOT = Path(__file__).resolve().parents[1]
DEFAULT_HISTORY = ROOT / "Temp" / "proposal_evaluation_history.json"
DEFAULT_LATE = ROOT / "Temp" / "late_chase_attribution_v1.json"
DEFAULT_REBOUND = ROOT / "Temp" / "rebound_sell_efficiency_v1.json"
DEFAULT_OUTCOME = ROOT / "Temp" / "outcome_quality_score_v1.json"
DEFAULT_EXEC_QUALITY = ROOT / "Temp" / "execution_quality_harness_v1.json"
DEFAULT_OUT = ROOT / "Temp" / "perf_recovery_harness_v1.json"


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


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--history", default=str(DEFAULT_HISTORY))
    ap.add_argument("--late", default=str(DEFAULT_LATE))
    ap.add_argument("--rebound", default=str(DEFAULT_REBOUND))
    ap.add_argument("--outcome", default=str(DEFAULT_OUTCOME))
    ap.add_argument("--execution-quality", default=str(DEFAULT_EXEC_QUALITY))
    ap.add_argument("--out", default=str(DEFAULT_OUT))
    args = ap.parse_args()

    hp = Path(args.history); lp = Path(args.late); rp = Path(args.rebound); op = Path(args.outcome); eqp = Path(args.execution_quality); outp = Path(args.out)
    for p in (hp, lp, rp, op, eqp, outp):
        if not p.is_absolute():
            p = ROOT / p

    hist = _load(hp if hp.is_absolute() else ROOT / hp)
    late = _load(lp if lp.is_absolute() else ROOT / lp)
    rebound = _load(rp if rp.is_absolute() else ROOT / rp)
    outcome = _load(op if op.is_absolute() else ROOT / op)
    exec_q = _load(eqp if eqp.is_absolute() else ROOT / eqp)

    recs = hist.get("records") if isinstance(hist.get("records"), list) else []
    t20 = [r for r in recs if isinstance(r, dict) and r.get("t20_evaluation_status") == "EVALUATED_T20"]
    watch = [r for r in t20 if str(r.get("action") or "").upper() == "WATCH"]
    watch_operational = [r for r in watch if str(r.get("validation_status") or "").upper() != "REPLAY_BACKFILL"]
    watch_replay = [r for r in watch if str(r.get("validation_status") or "").upper() == "REPLAY_BACKFILL"]
    watch_miss = [r for r in watch_operational if r.get("t20_outcome") == "MISMATCHED"]
    watch_miss_rate = round((len(watch_miss) / len(watch_operational)) * 100.0, 2) if watch_operational else 0.0

    late_status = str(late.get("status") or "DATA_MISSING")
    late_samples = int(late.get("samples") or 0)
    block_cnt = int((late.get("metrics") or {}).get("late_chase_blocked_count") or 0)
    high_risk_cnt = int((late.get("metrics") or {}).get("late_chase_high_risk_count") or 0)
    late_chase_block_precision = round((block_cnt / max(1, high_risk_cnt)) * 100.0, 2) if high_risk_cnt else 0.0

    value_damage = float((rebound.get("metrics") or {}).get("value_damage_pct_avg") or 0.0)
    out_m = outcome.get("metrics") if isinstance(outcome.get("metrics"), dict) else {}
    t20_pass_rate = float(out_m.get("t20_effective_rate") or out_m.get("t20_pass_rate") or 0.0)
    t20_source = str(out_m.get("t20_source") or "")
    outcome_score = float(outcome.get("score") or 0.0)
    has_operational_t20 = t20_source == "proposal_evaluation_history.operational_t20_only"
    exec_gate = str(exec_q.get("gate") or "DATA_MISSING")
    exec_oper = (exec_q.get("metrics") or {}).get("operational_t20") if isinstance((exec_q.get("metrics") or {}).get("operational_t20"), dict) else {}
    exec_expectancy = float(exec_oper.get("expectancy_pct") or 0.0)
    exec_mdd = float(exec_oper.get("max_drawdown_pct") or 0.0)
    exec_win_rate = float(exec_oper.get("win_rate_pct") or 0.0)

    gate = "PASS"
    reasons = []
    if has_operational_t20 and t20_pass_rate < 60.0:
        gate = "FAIL"
        reasons.append("T20_PASS_RATE_BELOW_60")
    if has_operational_t20 and value_damage > 10.0:
        gate = "FAIL"
        reasons.append("VALUE_DAMAGE_ABOVE_10")
    elif not has_operational_t20 and value_damage > 10.0:
        reasons.append("VALUE_DAMAGE_WATCH_UNTIL_OPERATIONAL_SAMPLE")
    if watch_operational and watch_miss_rate > 35.0:
        gate = "FAIL"
        reasons.append("WATCH_MISS_RATE_TOO_HIGH")
    if not watch_operational:
        reasons.append("WATCH_MISS_SAMPLE_INSUFFICIENT")
    if late_status == "WATCH_PENDING_SAMPLE" and late_samples < 30:
        reasons.append("LATE_CHASE_SAMPLE_INSUFFICIENT")
    if not has_operational_t20:
        reasons.append("T20_OPERATIONAL_SAMPLE_INSUFFICIENT")
    if exec_gate == "FAIL":
        gate = "FAIL"
        reasons.append("EXECUTION_QUALITY_FAIL")
    elif exec_gate == "WATCH_PENDING_SAMPLE":
        reasons.append("EXECUTION_QUALITY_SAMPLE_INSUFFICIENT")

    if gate == "PASS" and (
        "WATCH_MISS_SAMPLE_INSUFFICIENT" in reasons
        or "LATE_CHASE_SAMPLE_INSUFFICIENT" in reasons
        or "T20_OPERATIONAL_SAMPLE_INSUFFICIENT" in reasons
    ):
        gate = "WATCH_PENDING_SAMPLE"

    res = {
        "formula_id": "PERF_RECOVERY_HARNESS_V1",
        "gate": gate,
        "reasons": reasons,
        "metrics": {
            "t20_pass_rate": t20_pass_rate,
            "outcome_quality_score": outcome_score,
            "t20_source": t20_source,
            "watch_miss_rate": watch_miss_rate,
            "watch_eval_count": len(watch_operational),
            "watch_mismatch_count": len(watch_miss),
            "watch_replay_eval_count": len(watch_replay),
            "late_chase_block_precision": late_chase_block_precision,
            "late_chase_status": late_status,
            "late_chase_samples": late_samples,
            "rebound_sell_value_damage": value_damage,
            "execution_quality_gate": exec_gate,
            "execution_expectancy_pct": exec_expectancy,
            "execution_max_drawdown_pct": exec_mdd,
            "execution_win_rate_pct": exec_win_rate,
        },
        "targets": {
            "t20_pass_rate_min": 60.0,
            "outcome_quality_score_min": 60.0,
            "watch_miss_rate_max": 35.0,
            "rebound_sell_value_damage_max": 10.0,
            "execution_expectancy_pct_min": 0.0,
            "execution_max_drawdown_pct_max": 12.0,
            "execution_win_rate_pct_min": 45.0,
        },
    }
    out_file = outp if outp.is_absolute() else ROOT / outp
    out_file.parent.mkdir(parents=True, exist_ok=True)
    out_file.write_text(json.dumps(res, ensure_ascii=False, indent=2), encoding="utf-8")
    print(json.dumps(res, ensure_ascii=False, indent=2))
    return 0


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