import json
from pathlib import Path


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

EXPECTED = {
    ROOT / "Source" / "AgrarianGame" / "AgrarianSaveGame.h": [
        "struct FAgrarianSavedTileRegistryState",
        f"FName ActiveTileId = TEXT(\"{GROUND_ZERO_TILE_ID}\");",
        "FString RegistryPath = TEXT(\"Data/Tiles/ground_zero_tiles.json\");",
        "int32 RegistrySchemaVersion = 1;",
        "int32 GenerationVersion = 1;",
        "int32 PackageVersion = 0;",
        "FAgrarianSavedTileRegistryState TileRegistry;",
    ],
    ROOT / "Docs" / "PersistenceDesignDocument.md": [
        "`FAgrarianSavedTileRegistryState`",
        f"active tile `{GROUND_ZERO_TILE_ID}`",
        "`Data/Tiles/ground_zero_tiles.json`",
    ],
    ROOT / "AGRARIAN_DEVELOPMENT_ROADMAP.md": [
        "[x] Add initial tile registry persistence for Ground Zero.",
        "`FAgrarianSavedTileRegistryState`",
        "`Data/Tiles/ground_zero_tiles.json`",
    ],
}


def main() -> None:
    missing = []
    registry = json.loads((ROOT / "Data" / "Tiles" / "ground_zero_tiles.json").read_text(encoding="utf-8"))
    ground_zero = next((tile for tile in registry.get("tiles", []) if tile.get("tile_id") == GROUND_ZERO_TILE_ID), None)
    if not ground_zero:
        missing.append("Data/Tiles/ground_zero_tiles.json: Ground Zero tile record")
    else:
        if registry.get("schema_version") != 1:
            missing.append("Data/Tiles/ground_zero_tiles.json: schema_version 1")
        if ground_zero.get("generation_version") != 1:
            missing.append("Data/Tiles/ground_zero_tiles.json: generation_version 1")
        if ground_zero.get("package_version") != 0:
            missing.append("Data/Tiles/ground_zero_tiles.json: package_version 0")

    for path, snippets in EXPECTED.items():
        text = path.read_text(encoding="utf-8")
        for snippet in snippets:
            if snippet not in text:
                missing.append(f"{path.relative_to(ROOT)}: {snippet}")

    if missing:
        raise RuntimeError("Ground Zero tile registry persistence verification failed: " + "; ".join(missing))

    print("PASS: Ground Zero tile registry baseline is represented in save metadata.")


if __name__ == "__main__":
    main()
