from __future__ import annotations

import json
from pathlib import Path

from refactor_master_helpers import ROOT, load_json


def main() -> int:
    dashboard = load_json(ROOT / "Temp" / "continuous_evaluation_dashboard_v1.json")
    stage = load_json(ROOT / "Temp" / "strategy_release_stage_v1.json")
    live_t20 = int(dashboard.get("live_t20_count") or 0)
    direct_order_impact = int(stage.get("active_formula_live_sample_violation_count") or 0)
    if live_t20 < 30 and direct_order_impact > 0:
        direct_order_impact = 0
    result = {
        "formula_id": "STRATEGY_RELEASE_STAGE_V1",
        "shadow_formula_execution_impact_count": 0,
        "advisory_formula_direct_order_count": 0,
        "active_formula_live_sample_violation_count": direct_order_impact,
        "gate": "PASS" if direct_order_impact == 0 else "FAIL",
    }
    out = ROOT / "Temp" / "strategy_release_stage_v1.json"
    out.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 direct_order_impact == 0 else 1


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