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

import json
from pathlib import Path


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


def _read(rel: str) -> str:
    path = ROOT / rel
    if not path.exists():
        return ""
    return path.read_text(encoding="utf-8", errors="replace").lower()


def _require_any(text: str, options: list[str]) -> bool:
    return any(option.lower() in text for option in options)


def _require_all(text: str, groups: list[list[str]]) -> bool:
    return all(_require_any(text, group) for group in groups)


def main() -> int:
    files: dict[str, list[list[str]]] = {
        "AGENTS.md": [
            ["0b. 기본 하네스 완료 조건"],
            ["yaml"],
            ["코드"],
            ["데이터 실체"],
            ["검증 증빙"],
        ],
        "docs/runbook.md": [
            ["complete only when", "완료"],
            ["yaml"],
            ["code"],
            ["data artifact", "data artifacts", "data/artifact"],
            ["validation evidence", "검증 증빙"],
        ],
        "docs/ROADMAP_WBS.md": [
            ["완료 조건"],
            ["yaml"],
            ["코드"],
            ["데이터 실체"],
            ["검증 증빙"],
            ["wbs 작성"],
            ["성공판단 데이터"],
        ],
        "REPORT_GUIDE.md": [
            ["completion harness"],
            ["yaml"],
            ["code"],
            ["data artifact", "data artifacts", "data/artifact"],
            ["validation evidence", "검증 증빙"],
        ],
        "docs/doctrine.md": [
            ["completion harness"],
            ["yaml"],
            ["code"],
            ["data artifact", "data artifacts", "data/artifact"],
            ["validation evidence", "검증 증빙"],
        ],
        "prompts/analysis_prompt.md": [
            ["default completion harness"],
            ["yaml"],
            ["코드"],
            ["데이터 실체"],
            ["검증 증빙"],
            ["wbs"],
            ["성공판단"],
        ],
        "prompts/review_prompt.md": [
            ["default completion harness"],
            ["yaml"],
            ["code"],
            ["data artifact", "data/artifact"],
            ["validation evidence", "검증 증빙"],
            ["wbs"],
            ["success criteria", "성공판단"],
        ],
        "prompts/capture_parse_prompt.md": [
            ["기본 완료 조건"],
            ["yaml"],
            ["코드"],
            ["데이터 실체"],
            ["검증 증빙"],
            ["wbs"],
            ["성공판단"],
        ],
    }

    missing: list[dict[str, object]] = []
    for rel, groups in files.items():
        text = _read(rel)
        if not text:
            missing.append({"file": rel, "reason": "missing_file"})
            continue
        if not _require_all(text, groups):
            missing.append({"file": rel, "reason": "missing_required_text", "required_groups": groups})

    result = {
        "formula_id": "COMPLETION_HARNESS_INSTRUCTIONS_V1",
        "gate": "PASS" if not missing else "FAIL",
        "checked_files": len(files),
        "missing": missing,
    }
    out = ROOT / "Temp" / "completion_harness_instructions_v1.json"
    out.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())
