#!/usr/bin/env python3
"""Validate MVP disconnect/reconnect snapshot handling."""

from pathlib import Path
import sys

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


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


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


def main() -> int:
    errors: list[str] = []
    checks = {
        "Source/AgrarianGame/AgrarianPersistenceSubsystem.h": [
            "SavePlayerSnapshot",
            "RestorePlayerSnapshot",
            "CapturePlayerIntoSave",
            "RestorePlayerFromSave",
        ],
        "Source/AgrarianGame/AgrarianPersistenceSubsystem.cpp": [
            "bool UAgrarianPersistenceSubsystem::SavePlayerSnapshot",
            "bool UAgrarianPersistenceSubsystem::RestorePlayerSnapshot",
            "SaveGame->Players.RemoveAll",
            "SavedPlayer.Transform = Character->GetActorTransform()",
            "SavedPlayer.Survival = SurvivalComponent->Survival",
            "SavedPlayer.CareHistory = SurvivalComponent->CareHistory",
            "SavedPlayer.Inventory = InventoryComponent->Items",
            "Character->SetActorTransform",
            "InventoryComponent->RestoreSavedItems",
        ],
        "Source/AgrarianGame/AgrarianGameGameMode.h": [
            "virtual void RestartPlayer(AController* NewPlayer) override;",
            "virtual void Logout(AController* Exiting) override;",
        ],
        "Source/AgrarianGame/AgrarianGameGameMode.cpp": [
            "RestorePlayerSnapshot(AgrarianCharacter)",
            "SavePlayerSnapshot(AgrarianCharacter)",
            "Super::RestartPlayer(NewPlayer)",
            "Super::Logout(Exiting)",
        ],
        "Docs/MultiplayerNetworkingDesign.md": [
            "player reconnect snapshot",
            "SavePlayerSnapshot",
            "RestorePlayerSnapshot",
            "normal MVP spawn point",
        ],
        "AGRARIAN_DEVELOPMENT_ROADMAP.md": [
            "[x] Add disconnect/reconnect handling.",
            "MVP player reconnect snapshots",
        ],
    }

    for relative_path, needles in checks.items():
        try:
            content = read(relative_path)
            for needle in needles:
                require(content, needle, relative_path)
        except AssertionError as exc:
            errors.append(str(exc))

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

    print("PASS: MVP disconnect/reconnect snapshot handling is wired and documented.")
    return 0


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