#!/usr/bin/env python3
from __future__ import annotations

import json
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
AGENTS = ROOT / "AGENTS.md"
REPORT = ROOT / "Temp" / "document_search_exclusion_v1.json"

REQUIRED_EXCLUDED_PATHS = [
    "docs/archive/",
    "suggest/",
    "artifacts/archive/",
]


def main() -> int:
    text = AGENTS.read_text(encoding="utf-8", errors="replace") if AGENTS.exists() else ""
    missing = [path for path in REQUIRED_EXCLUDED_PATHS if path not in text]

    archive_candidates = []
    for rel in REQUIRED_EXCLUDED_PATHS:
        root = ROOT / rel.rstrip("/")
        if root.exists():
            archive_candidates.extend(sorted(str(p.relative_to(ROOT)).replace("\\", "/") for p in root.rglob("*") if p.is_file()))

    result = {
        "formula_id": "DOCUMENT_SEARCH_EXCLUSION_V1",
        "gate": "PASS" if not missing else "FAIL",
        "required_excluded_paths": REQUIRED_EXCLUDED_PATHS,
        "missing_policy_markers": missing,
        "archive_candidate_count": len(archive_candidates),
        "archive_candidates_sample": archive_candidates[:20],
    }
    REPORT.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 not missing else 1


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