from pathlib import Path
import json


ROOT = Path(__file__).resolve().parents[1]
GAME_STATE_H = ROOT / "Source" / "AgrarianGame" / "AgrarianGameState.h"
GAME_STATE_CPP = ROOT / "Source" / "AgrarianGame" / "AgrarianGameState.cpp"
GENERATOR = ROOT / "Scripts" / "generate_tile_solar_metadata.py"
SOLAR_METADATA = ROOT / "Data" / "Tiles" / "tile_solar_metadata.json"
SCHEMA = ROOT / "Data" / "Tiles" / "tile_registry.schema.json"
SQL = ROOT / "Data" / "Tiles" / "tile_registry.sql"
ROADMAP = ROOT / "AGRARIAN_DEVELOPMENT_ROADMAP.md"


EXPECTED = {
    GAME_STATE_H: [
        "ActiveSolarTileId",
        "ActiveTileLatitude",
        "ActiveTileLongitude",
        "ActiveTileTimeZoneId",
        "ActiveTileUtcOffsetHours",
        "ActiveDayOfYear",
        "SunriseHourLocal",
        "SunsetHourLocal",
        "SolarNoonHourLocal",
        "DayLengthHours",
        "ConfigureActiveSolarTile",
    ],
    GAME_STATE_CPP: [
        "UpdateSolarTimes();",
        "DOREPLIFETIME(AAgrarianGameState, SunriseHourLocal);",
        "return WorldHours < SunriseHourLocal || WorldHours > SunsetHourLocal;",
        "bool AAgrarianGameState::ConfigureActiveSolarTile",
        "void AAgrarianGameState::UpdateSolarTimes()",
        "NOAA",
        "EquationOfTimeMinutes",
        "SolarDeclinationRadians",
    ],
    GENERATOR: [
        "SOLAR_READY_STATUSES",
        "TIMEZONE_OVERRIDES",
        "Only tiles with real source status",
        "gz_us_ca_pacifica_utm10n_e544_n4160",
    ],
    SCHEMA: ["solar_metadata", "standard_utc_offset_hours", "daylight_utc_offset_hours"],
    SQL: ["terrain_tile_solar_metadata", "time_zone_id", "solar_model"],
    ROADMAP: ["Add real local time-zone and sunrise/sunset lookup for Ground Zero"],
}


def assert_contains():
    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("Tile solar time verification failed: " + "; ".join(missing))


def assert_solar_metadata():
    data = json.loads(SOLAR_METADATA.read_text(encoding="utf-8"))
    tiles = data.get("tiles", [])
    if len(tiles) != 1:
        raise RuntimeError(f"Expected exactly one generated solar tile, got {len(tiles)}")
    tile = tiles[0]
    if tile.get("tile_id") != "gz_us_ca_pacifica_utm10n_e544_n4160":
        raise RuntimeError(f"Unexpected solar tile id: {tile.get('tile_id')}")
    if tile.get("time_zone_id") != "America/Los_Angeles":
        raise RuntimeError("Ground Zero timezone metadata is missing or incorrect")
    june = tile.get("sample_solar_hours", {}).get("june_solstice", {})
    if not (5.0 <= june.get("sunrise_hour", -1) <= 6.5 and 20.0 <= june.get("sunset_hour", -1) <= 21.0):
        raise RuntimeError(f"Ground Zero June solar sample is outside expected range: {june}")


def main():
    assert_contains()
    assert_solar_metadata()
    print("Agrarian tile solar time verification complete.")


if __name__ == "__main__":
    main()
