from __future__ import annotations

import json
from pathlib import Path

from refactor_master_helpers import ROOT, load_json


# Individual validators are now DAG nodes (not separate npm scripts) after P02_T01 script budget reduction.
# Only the release entry-points and strict gate need to be present as scripts.
REQUIRED_ORDER = ["full-gate", "validate-engine-strict", "ops:validate", "ops:release"]


def main() -> int:
    pkg = load_json(ROOT / "package.json")
    scripts = pkg.get("scripts", {}) if isinstance(pkg.get("scripts"), dict) else {}
    script_text = "\n".join(f"{k}={v}" for k, v in scripts.items())
    missing = [name for name in REQUIRED_ORDER if name not in script_text]
    skip_default = [k for k, v in scripts.items() if isinstance(v, str) and "--skip-validate" in v]
    result = {
        "formula_id": "RELEASE_GATE_SEQUENCE_V1",
        "release_gate_sequence_status": "OK" if not missing and not skip_default else "FAIL",
        "skip_validate_default_count": len(skip_default),
        "strict_gate_contains_full_gate": "full-gate" in script_text,
        "missing_required_scripts": missing,
        "gate": "PASS" if not missing and not skip_default else "FAIL",
    }
    out = ROOT / "Temp" / "release_gate_sequence_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 result["gate"] == "PASS" else 1


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