#!/usr/bin/env python3
import sys
import json
import argparse
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--json", default="Temp/shadow_ledger_v2.json")
    parser.add_argument("--strict", action="store_true")
    args = parser.parse_args()

    json_path = ROOT / args.json
    if not json_path.exists():
        print(f"Shadow ledger not found: {json_path}")
        sys.exit(1)

    data = json.loads(json_path.read_text(encoding="utf-8"))
    formulas = data.get("shadow_formulas", [])

    active_without_shadow_history = 0
    promotion_without_change_request = 0

    for f in formulas:
        state = f.get("lifecycle_state")
        promotion_allowed = f.get("promotion_allowed", False)
        
        # If candidate state but promotion is not allowed
        if state == "candidate" and not promotion_allowed:
            active_without_shadow_history += 1

    if active_without_shadow_history > 0:
        print(f"Validation failed: {active_without_shadow_history} formulas in candidate state without meeting promotion gates")
        sys.exit(1)

    print("VALIDATION OK")
    sys.exit(0)

if __name__ == "__main__":
    main()
