from pathlib import Path
import json


ROOT = Path(__file__).resolve().parents[1]
BUILD_CS = ROOT / "Source" / "AgrarianGame" / "AgrarianGame.Build.cs"
HEADER = ROOT / "Source" / "AgrarianGame" / "AgrarianWeatherProviderSubsystem.h"
CPP = ROOT / "Source" / "AgrarianGame" / "AgrarianWeatherProviderSubsystem.cpp"
GENERATOR = ROOT / "Scripts" / "generate_tile_weather_manifest.py"
MANIFEST = ROOT / "Data" / "Tiles" / "tile_weather_manifest.json"
TDD = ROOT / "Docs" / "TechnicalDesignDocument.md"
ROADMAP = ROOT / "AGRARIAN_DEVELOPMENT_ROADMAP.md"


EXPECTED = {
    BUILD_CS: ['"HTTP"', '"Json"'],
    HEADER: [
        "UAgrarianWeatherProviderSubsystem",
        "FAgrarianWeatherProviderSnapshot",
        "RequestWeatherForActiveGameState",
        "RequestWeatherForTile",
        "BuildOpenMeteoForecastUrl",
        "ApplySnapshotToGameState",
        "MapOpenMeteoWeatherCode",
    ],
    CPP: [
        "current=temperature_2m,relative_humidity_2m,precipitation",
        "daily=temperature_2m_max,temperature_2m_min",
        "GameState->ApplyMappedWeatherInputs",
        "MapOpenMeteoWeatherCode",
    ],
    GENERATOR: [
        "WEATHER_READY_STATUSES",
        "Every source-backed/generated tile",
        "weather_lookup_metadata",
        "tile_weather_manifest.json",
    ],
    TDD: [
        "Open-Meteo",
        "tile center latitude/longitude",
        "Future source-backed",
    ],
    ROADMAP: [
        "[x] Add real-world weather provider adapter for Ground Zero by latitude/longitude.",
        "tile-driven Open-Meteo adapter",
    ],
}


def assert_contains() -> None:
    missing = []
    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("Weather provider adapter verification failed: " + "; ".join(missing))


def assert_manifest() -> None:
    data = json.loads(MANIFEST.read_text(encoding="utf-8"))
    tiles = data.get("tiles", [])
    if len(tiles) != 1:
        raise RuntimeError(f"Expected one source-backed weather tile, got {len(tiles)}")
    tile = tiles[0]
    if tile.get("tile_id") != "gz_us_ca_pacifica_utm10n_e544_n4160":
        raise RuntimeError(f"Unexpected weather tile id: {tile.get('tile_id')}")
    if tile.get("center_latitude") is None or tile.get("center_longitude") is None:
        raise RuntimeError("Weather manifest tile is missing coordinates")


def main() -> None:
    assert_contains()
    assert_manifest()
    print("Agrarian weather provider adapter verification complete.")


if __name__ == "__main__":
    main()
