#!/usr/bin/env python3
from __future__ import annotations

import json
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent

def main() -> int:
    accuracy_path = ROOT / "Temp" / "prediction_accuracy_harness_v2.json"
    honest_path = ROOT / "Temp" / "honest_performance_guard_v1.json"

    if not accuracy_path.exists():
        print(f"accuracy harness file missing: {accuracy_path}")
        return 1
    if not honest_path.exists():
        print(f"honest guard file missing: {honest_path}")
        return 1

    try:
        acc_data = json.loads(accuracy_path.read_text(encoding="utf-8"))
        honest_data = json.loads(honest_path.read_text(encoding="utf-8"))
    except Exception as e:
        print(f"Failed to parse json: {e}")
        return 1

    errors = []

    # 1. factor_outcome_join_rate_pct >= 95
    audit = acc_data.get("data_origin_audit", {})
    op_count = audit.get("operational_sample_count", 0)
    untagged = audit.get("untagged_row_count", 0)
    
    if op_count > 0:
        factor_outcome_join_rate_pct = 100.0 * (1.0 - (untagged / op_count))
    else:
        factor_outcome_join_rate_pct = 100.0

    if factor_outcome_join_rate_pct < 95.0:
        errors.append(f"factor_outcome_join_rate_pct is {factor_outcome_join_rate_pct:.2f}% (Expected >= 95%)")

    # 2. live_sample_under_30_unlock_count == 0
    live_sample_under_30_unlock_count = 0
    calibration_state = acc_data.get("calibration_state", "")
    t5_sample = acc_data.get("t5_sample", 0)
    
    if t5_sample < 30 and calibration_state not in ("INSUFFICIENT_SAMPLES", "UNKNOWN", ""):
        if calibration_state == "CALIBRATED":
            live_sample_under_30_unlock_count += 1
            errors.append(f"t5_sample={t5_sample} < 30 but calibration_state is unlocked ({calibration_state})")

    # 3. replay_live_mixed_metric_count == 0
    replay_live_mixed_metric_count = 0
    replay_in_live = audit.get("replay_in_live_stats", 0)
    if replay_in_live > 0:
        replay_live_mixed_metric_count += 1
        errors.append(f"Replay samples mixed in live stats: {replay_in_live}")

    gate_passed = (factor_outcome_join_rate_pct >= 95.0) and \
                  (live_sample_under_30_unlock_count == 0) and \
                  (replay_live_mixed_metric_count == 0)

    result = {
        "formula_id": "HONEST_PERFORMANCE_GUARD_VALIDATOR_V1",
        "factor_outcome_join_rate_pct": factor_outcome_join_rate_pct,
        "live_sample_under_30_unlock_count": live_sample_under_30_unlock_count,
        "replay_live_mixed_metric_count": replay_live_mixed_metric_count,
        "errors": errors,
        "gate": "PASS" if gate_passed else "FAIL"
    }

    # Write output to Temp
    out_dir = ROOT / "Temp"
    out_dir.mkdir(parents=True, exist_ok=True)
    out_path = out_dir / "honest_performance_guard_validation_v1.json"
    out_path.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")

    print(json.dumps(result, ensure_ascii=True, indent=2))
    return 0 if gate_passed else 1

if __name__ == "__main__":
    sys.exit(main())
