from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
FILES = {
    "AgrarianSurvivalComponent.h": ROOT / "Source" / "AgrarianGame" / "AgrarianSurvivalComponent.h",
    "AgrarianSurvivalComponent.cpp": ROOT / "Source" / "AgrarianGame" / "AgrarianSurvivalComponent.cpp",
    "AgrarianPersistenceSubsystem.h": ROOT / "Source" / "AgrarianGame" / "AgrarianPersistenceSubsystem.h",
    "AgrarianPersistenceSubsystem.cpp": ROOT / "Source" / "AgrarianGame" / "AgrarianPersistenceSubsystem.cpp",
    "AgrarianGamePlayerController.cpp": ROOT / "Source" / "AgrarianGame" / "AgrarianGamePlayerController.cpp",
    "PersistenceDesignDocument.md": ROOT / "Docs" / "PersistenceDesignDocument.md",
}

EXPECTED = {
    "AgrarianSurvivalComponent.h": [
        "void ApplySavedState(const FAgrarianSurvivalSnapshot& SavedSurvival, const FAgrarianCareHistorySnapshot& SavedCareHistory);",
    ],
    "AgrarianSurvivalComponent.cpp": [
        "void UAgrarianSurvivalComponent::ApplySavedState",
        "Survival = SavedSurvival;",
        "CareHistory = SavedCareHistory;",
        "ClampSurvival();",
        "ClampCareHistory();",
    ],
    "AgrarianPersistenceSubsystem.h": [
        "int32 CapturePlayers(UAgrarianSaveGame* SaveGame) const;",
        "int32 RestorePlayers(const UAgrarianSaveGame* SaveGame) const;",
        "void FindAgrarianPlayers(TArray<AAgrarianGameCharacter*>& OutPlayers) const;",
        "FString GetPlayerPersistenceId(const AAgrarianGameCharacter* Character) const;",
    ],
    "AgrarianPersistenceSubsystem.cpp": [
        "#include \"AgrarianInventoryComponent.h\"",
        "int32 UAgrarianPersistenceSubsystem::CapturePlayers",
        "SavedPlayer.Survival = SurvivalComponent->Survival;",
        "SavedPlayer.CareHistory = SurvivalComponent->CareHistory;",
        "SavedPlayer.Inventory = InventoryComponent->Items;",
        "int32 UAgrarianPersistenceSubsystem::RestorePlayers",
        "SurvivalComponent->ApplySavedState(SavedPlayer->Survival, SavedPlayer->CareHistory);",
        "InventoryComponent->Items = SavedPlayer->Inventory;",
        "CapturePlayers(SaveGame);",
        "void UAgrarianPersistenceSubsystem::FindAgrarianPlayers",
        "FString UAgrarianPersistenceSubsystem::GetPlayerPersistenceId",
    ],
    "AgrarianGamePlayerController.cpp": [
        "const int32 RestoredPlayerCount = Persistence->RestorePlayers(SaveGame);",
        "Restored players: %d. Restored actors: %d.",
    ],
    "PersistenceDesignDocument.md": [
        "captures live Agrarian player characters",
        "RestorePlayers",
    ],
}


def main():
    missing = []
    for label, path in FILES.items():
        text = path.read_text(encoding="utf-8")
        for snippet in EXPECTED[label]:
            if snippet not in text:
                missing.append(f"{label}: {snippet}")

    if missing:
        raise RuntimeError("Stat save/load verification failed: " + "; ".join(missing))

    print("Agrarian stat save/load verification complete.")


if __name__ == "__main__":
    main()
