"""build_vacuous_pass_audit_v1.py — NON_VACUOUS_PASS_GUARD_V1

RC2 수정: row_count=0 / sample_n < min_samples 게이트가 PASS로 집계되어
점수를 부풀리는 문제 감지. 미달 게이트를 WATCH_PENDING_SAMPLE로 강제 강등.
"""
from __future__ import annotations

import json
import re
from pathlib import Path

from v7_hardening_common import ROOT, TEMP, save_json

MIN_SAMPLES_DEFAULT = 30

EFFECTIVE_N_FIELDS = ["sample_count", "row_count", "evaluated_count", "samples", "n",
                      "sample_n", "cases_analyzed", "t5_sample", "t20_sample",
                      "op_t5_sample", "op_t20_sample"]

KNOWN_VACUOUS_GATES = [
    # (file_stem, gate_field, n_field, label)
    ("value_preservation_scorer_v2", "gate", "row_count", "VALUE_PRESERVATION_SCORER_V2"),
    ("rebound_sell_efficiency_v1", "gate", "sample_n", "rebound_efficiency_score"),
    ("late_rebound_bucket_score_v1", "gate", "sample_n", "late_rebound_bucket_score"),
    ("late_chase_attribution_v4", "gate", "samples", "late_chase_attribution_v4"),
    ("smart_money_liquidity_evidence_gate_v5", "gate", "sample_count", "smart_money_liquidity_evidence"),
    ("live_trade_outcome_ledger_v1", "gate", "live_count", "live_trade_outcome_ledger"),
]


def _get_effective_n(obj: dict) -> int | None:
    for field in EFFECTIVE_N_FIELDS:
        val = obj.get(field)
        if val is not None:
            try:
                return int(val)
            except (TypeError, ValueError):
                pass
    return None


def _audit_known_gates(violations: list, demotions: list) -> None:
    for file_stem, gate_field, n_field, label in KNOWN_VACUOUS_GATES:
        path = TEMP / f"{file_stem}.json"
        if not path.exists():
            continue
        try:
            obj = json.loads(path.read_text(encoding="utf-8"))
        except Exception:
            continue

        gate_val = str(obj.get(gate_field) or "").upper()
        n_val = _get_effective_n({n_field: obj.get(n_field)})
        if n_val is None:
            n_val = _get_effective_n(obj)

        if n_val is None:
            n_val = 0

        is_pass = gate_val == "PASS"
        is_low_n = n_val < MIN_SAMPLES_DEFAULT

        if is_pass and is_low_n:
            label_str = f"[PASS_INVALID_LOW_N: n={n_val} < {MIN_SAMPLES_DEFAULT}]"
            violations.append({
                "formula": label,
                "file": str(path.name),
                "gate_field": gate_field,
                "gate_value": gate_val,
                "effective_n": n_val,
                "min_samples": MIN_SAMPLES_DEFAULT,
                "required_demotion": "WATCH_PENDING_SAMPLE",
                "label": label_str,
            })
        elif is_low_n and gate_val not in ("", "UNKNOWN"):
            demotions.append({
                "formula": label,
                "file": str(path.name),
                "gate_field": gate_field,
                "gate_value": gate_val,
                "effective_n": n_val,
                "min_samples": MIN_SAMPLES_DEFAULT,
                "status": "ALREADY_NON_PASS" if not is_pass else "WATCH_PENDING_SAMPLE",
            })


def main() -> int:
    violations: list = []
    demotions: list = []

    _audit_known_gates(violations, demotions)

    vacuous_pass_count = len(violations)
    gate = "PASS" if vacuous_pass_count == 0 else "FAIL"

    result = {
        "formula_id": "NON_VACUOUS_PASS_GUARD_V1",
        "gate": gate,
        "vacuous_pass_gate_count": vacuous_pass_count,
        "min_samples_default": MIN_SAMPLES_DEFAULT,
        "violations": violations,
        "demotions_already_non_pass": demotions,
        "enforcement_note": (
            "PASS 분자에 포함 금지: " + ", ".join(v["formula"] for v in violations)
            if violations else "공허한 PASS 게이트 없음"
        ),
    }
    save_json(str(TEMP / "vacuous_pass_audit_v1.json"), result)
    print(json.dumps(result, ensure_ascii=False, indent=2))
    return 0 if gate == "PASS" else 1


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