#!/usr/bin/env python3
"""Validate native map boundary source and roadmap/doc wiring."""

from pathlib import Path
import sys

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


def read_text(relative_path: str) -> str:
    path = REPO_ROOT / relative_path
    if not path.exists():
        raise AssertionError(f"Missing required file: {relative_path}")
    return path.read_text(encoding="utf-8")


def require(needle: str, haystack: str, context: str) -> None:
    if needle not in haystack:
        raise AssertionError(f"Missing {needle!r} in {context}")


def main() -> int:
    errors: list[str] = []

    try:
        header = read_text("Source/AgrarianGame/AgrarianMapBoundaryVolume.h")
        for marker in [
            "AAgrarianMapBoundaryVolume",
            "BoundaryVolume",
            "BoundaryId",
            "bClampPlayersAtBoundary",
            "BoundaryPaddingCm",
            "WarningDistanceCm",
            "IsLocationOutsideBoundary",
            "IsLocationInsideWarningZone",
            "ClampLocationToBoundary",
        ]:
            require(marker, header, "AgrarianMapBoundaryVolume.h")
    except AssertionError as exc:
        errors.append(str(exc))

    try:
        source = read_text("Source/AgrarianGame/AgrarianMapBoundaryVolume.cpp")
        for marker in [
            "UGameplayStatics::GetAllActorsOfClass",
            "AAgrarianGameCharacter::StaticClass()",
            "TeleportTo",
            "StopMovementImmediately",
            "BoundaryVolume->SetBoxExtent(FVector(50000.0f, 50000.0f, 25000.0f))",
        ]:
            require(marker, source, "AgrarianMapBoundaryVolume.cpp")
    except AssertionError as exc:
        errors.append(str(exc))

    try:
        setup = read_text("Scripts/setup_ground_zero_demo_map.py")
        for marker in [
            "MAP_BOUNDARY_CONFIG",
            "AGR_GroundZeroMapBoundary",
            "unreal.AgrarianMapBoundaryVolume",
            "spawn_map_boundary_volume()",
        ]:
            require(marker, setup, "Scripts/setup_ground_zero_demo_map.py")
    except AssertionError as exc:
        errors.append(str(exc))

    try:
        roadmap = read_text("AGRARIAN_DEVELOPMENT_ROADMAP.md")
        require("[x] Add map boundaries or soft limits.", roadmap, "AGRARIAN_DEVELOPMENT_ROADMAP.md")
        docs = read_text("Docs/TechnicalDesignDocument.md")
        require("AGR_GroundZeroMapBoundary", docs, "Docs/TechnicalDesignDocument.md")
        require("AAgrarianMapBoundaryVolume", docs, "Docs/TechnicalDesignDocument.md")
    except AssertionError as exc:
        errors.append(str(exc))

    if errors:
        for error in errors:
            print(f"ERROR: {error}", file=sys.stderr)
        return 1

    print("Map boundary source is wired.")
    return 0


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