from __future__ import annotations

import json
from pathlib import Path

from refactor_master_helpers import ROOT, load_json


def main() -> int:
    candidates = [
        ROOT / "Temp" / "final_context_for_llm_v3.json",
        ROOT / "Temp" / "final_context_for_llm_v2.json",
        ROOT / "Temp" / "final_context_for_llm_v1.json",
    ]
    source = next((p for p in candidates if p.exists()), None)
    data = load_json(source) if source else {}
    result = {
        "formula_id": "FINAL_CONTEXT_FOR_LLM_V1",
        "renderer_input_count": 1 if source else 0,
        "final_context_schema_status": "OK" if source else "MISSING",
        "deprecated_final_context_versions_not_used_by_renderer": True,
        "source_file": str(source) if source else "DATA_MISSING",
        "gate": "PASS" if source else "FAIL",
    }
    out = ROOT / "Temp" / "final_context_for_llm_v1_validation.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 source else 1


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