from __future__ import annotations

import json
from pathlib import Path
import yaml

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


def main() -> int:
    # 1. Load canonical formulas from spec/13_formula_registry.yaml
    registry_path = ROOT / "spec" / "13_formula_registry.yaml"
    if not registry_path.exists():
        print(f"Registry not found: {registry_path}")
        return 1
    registry_data = yaml.safe_load(registry_path.read_text(encoding="utf-8"))
    canonical_formulas = registry_data.get("formula_registry", {}).get("formulas", {})
    canonical_set = set(canonical_formulas.keys())

    # 2. Load domain formulas from spec/formulas/domains/*.yaml
    domain_dir = ROOT / "spec" / "formulas" / "domains"
    domain_formulas = {}
    duplicate_formula_count = 0

    for path in sorted(domain_dir.glob("*.yaml")):
        if path.name == "manifest.yaml":
            continue
        try:
            doc = yaml.safe_load(path.read_text(encoding="utf-8")) or {}
        except Exception as e:
            print(f"Error parsing {path}: {e}")
            continue
        formulas_in_doc = doc.get("formulas") if isinstance(doc.get("formulas"), dict) else {}
        for fid, row in formulas_in_doc.items():
            if fid in domain_formulas:
                duplicate_formula_count += 1
            domain_formulas[fid] = row

    domain_set = set(domain_formulas.keys())

    # Calculate missing
    missing_in_domain = canonical_set - domain_set
    missing_in_registry = domain_set - canonical_set

    formula_domain_missing_count = len(missing_in_domain) + len(missing_in_registry)

    # 3. Check duplicate threshold definitions in spec/calibration_registry.yaml
    calibration_path = ROOT / "spec" / "calibration_registry.yaml"
    duplicate_threshold_definition_count = 0
    if calibration_path.exists():
        try:
            calib_data = yaml.safe_load(calibration_path.read_text(encoding="utf-8")) or {}
            calib_items = calib_data.get("calibration_registry", [])
            seen_calib = set()
            for item in calib_items:
                cid = item.get("id")
                if cid:
                    if cid in seen_calib:
                        duplicate_threshold_definition_count += 1
                    seen_calib.add(cid)
        except Exception as e:
            print(f"Error parsing calibration registry: {e}")

    gate = "PASS" if (formula_domain_missing_count == 0 and duplicate_formula_count == 0 and duplicate_threshold_definition_count == 0) else "FAIL"

    result = {
        "formula_id": "FORMULA_REGISTRY_SYNC_V1",
        "canonical_formula_count": len(canonical_set),
        "domain_formula_count": len(domain_set),
        "formula_domain_missing_count": formula_domain_missing_count,
        "duplicate_formula_count": duplicate_formula_count,
        "duplicate_threshold_definition_count": duplicate_threshold_definition_count,
        "gate": gate,
        "missing_in_domain": sorted(list(missing_in_domain)),
        "missing_in_registry": sorted(list(missing_in_registry))
    }

    out_path = ROOT / "Temp" / "formula_registry_sync_v1.json"
    out_path.parent.mkdir(parents=True, exist_ok=True)
    out_path.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")

    print(json.dumps(result, ensure_ascii=False, indent=2))
    return 0 if gate == "PASS" else 1


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