#!/usr/bin/env python3
"""Verify the MVP startup/menu flow is segmented and modal."""

from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
FRONTEND_H = ROOT / "Source" / "AgrarianGame" / "AgrarianMvpFrontendWidget.h"
FRONTEND_CPP = ROOT / "Source" / "AgrarianGame" / "AgrarianMvpFrontendWidget.cpp"
CONTROLLER_CPP = ROOT / "Source" / "AgrarianGame" / "AgrarianGamePlayerController.cpp"
ROADMAP = ROOT / "AGRARIAN_DEVELOPMENT_ROADMAP.md"


def require(condition: bool, message: str) -> None:
    if not condition:
        raise SystemExit(f"FAILED: {message}")


def main() -> None:
    header = FRONTEND_H.read_text(encoding="utf-8")
    frontend = FRONTEND_CPP.read_text(encoding="utf-8")
    controller = CONTROLLER_CPP.read_text(encoding="utf-8")
    roadmap = ROADMAP.read_text(encoding="utf-8")

    for token in (
        "SavingAndQuit",
        "ExecuteSaveAndQuit",
    ):
        require(token in header, f"missing segmented flow declaration: {token}")

    for token in (
        "Character Selection",
        "Server Join",
        "Loading Segment",
        "Pause Menu",
        "Gameplay is paused while this menu is active.",
        "Saving World",
        "Writing the current world state",
        "SetActiveScreen(EAgrarianMvpFrontendScreen::SavingAndQuit)",
        "GetTimerManager().SetTimer",
        "ExecuteSaveAndQuit",
        "ConsoleCommand(TEXT(\"AgrarianSaveWorld\"))",
        "ConsoleCommand(TEXT(\"quit\"))",
    ):
        require(token in frontend, f"missing segmented frontend token: {token}")

    require(
        controller.count("FInputModeUIOnly") >= 3,
        "startup/frontend/pause paths should use UI-only input while menus are active",
    )
    for token in (
        "SetIgnoreMoveInput(true)",
        "SetIgnoreLookInput(true)",
        "SetInputMode(FInputModeGameOnly())",
        "SetIgnoreMoveInput(false)",
        "SetIgnoreLookInput(false)",
        "saving",
    ):
        require(token in controller + frontend, f"missing modal input or debug token: {token}")

    require(
        "- [x] Make startup credits, character selection, server/join, loading, pause, save, and quit feel like separate intentional segments" in roadmap,
        "0.1.O segmented startup/menu roadmap item is not checked off",
    )

    print("OK: MVP startup, menu, loading, pause, save, and quit flow is segmented and modal.")


if __name__ == "__main__":
    main()
