#!/usr/bin/env python3
"""Verify 0.1 is complete and 0.2 readiness notes are present."""

from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
ROADMAP = ROOT / "AGRARIAN_DEVELOPMENT_ROADMAP.md"
REVIEW = ROOT / "Docs" / "CodebaseReadinessReview.md"

REQUIRED_ROADMAP_SNIPPETS = [
    "# Version 0.2 - Persistent Homesteading",
    "Engineering posture for 0.2:",
    "Define claim data schema with claim ID",
    "Define crop data asset schema",
    "Define animal data asset schema",
    "Define storage data schema",
    "Define structure piece data asset schema",
    "Define transaction record schema",
    "Convert the 0.1.R knowledge foundation into first data records",
    "## 0.2.I 0.1 Hardening And Technical Debt",
    "Split `AgrarianEditorAutomationLibrary.cpp`",
    "Create a grouped verifier runner",
    "Separate admin/dev console command authority",
    "## 0.2.J Homesteading Visual And Biome Realism Pass",
    "Replace tan/flat terrain presentation",
    "Add investor visual QA screenshots",
]

REQUIRED_REVIEW_SNIPPETS = [
    "# Codebase Readiness Review",
    "All `0.1.A` through `0.1.R` roadmap checkboxes are complete.",
    "AgrarianEditorAutomationLibrary.cpp` is doing too much",
    "Low-Risk Cleanup Applied",
    "Removed duplicate `SavingAndQuit` branch logic",
    "0.2 Engineering Priorities",
]


def get_0_1_text(roadmap: str) -> str:
    start = roadmap.find("## 0.1.A ")
    end = roadmap.find("# Version 0.2")
    if start == -1 or end == -1 or end <= start:
        raise SystemExit("FAILED: could not isolate 0.1 roadmap section")
    return roadmap[start:end]


def main() -> None:
    roadmap = ROADMAP.read_text(encoding="utf-8")
    review = REVIEW.read_text(encoding="utf-8")

    failures: list[str] = []

    section_0_1 = get_0_1_text(roadmap)
    unchecked_0_1 = [
        line.strip()
        for line in section_0_1.splitlines()
        if line.strip().startswith("- [ ]")
    ]
    if unchecked_0_1:
        failures.append("0.1 has unchecked items: " + "; ".join(unchecked_0_1[:5]))

    for snippet in REQUIRED_ROADMAP_SNIPPETS:
        if snippet not in roadmap:
            failures.append(f"roadmap missing {snippet!r}")

    for snippet in REQUIRED_REVIEW_SNIPPETS:
        if snippet not in review:
            failures.append(f"readiness review missing {snippet!r}")

    if failures:
        raise SystemExit("FAILED: " + "; ".join(failures))

    print("OK: 0.1 completion and 0.2 readiness are documented.")


if __name__ == "__main__":
    main()
