from __future__ import annotations

import argparse
from pathlib import Path

import yaml


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


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("--strict", action="store_true")
    args = parser.parse_args()
    manifest = yaml.safe_load((ROOT / "spec" / "formulas" / "manifest.yaml").read_text(encoding="utf-8"))
    normalized = yaml.safe_load((ROOT / "spec" / "03_formulas" / "formula_registry.normalized.yaml").read_text(encoding="utf-8"))
    domain_files = manifest.get("domains") or {}
    formulas = normalized.get("formulas") or []
    missing_owner = [row.get("formula_id") for row in formulas if not str((row or {}).get("owner") or "").strip()]
    missing_status = [row.get("formula_id") for row in formulas if not str((row or {}).get("status") or "").strip()]
    missing_outputs = [row.get("formula_id") for row in formulas if not (row or {}).get("output_fields")]
    ok = bool(domain_files) and len(formulas) > 0 and not missing_owner and not missing_status and not missing_outputs
    print("FORMULA_REGISTRY_OK" if ok else "FORMULA_REGISTRY_FAIL")
    print(f"domain_count={len(domain_files)} formula_count={len(formulas)} missing_owner={len(missing_owner)} missing_status={len(missing_status)} missing_outputs={len(missing_outputs)}")
    return 0 if ok or not args.strict else 1


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