from pathlib import Path
import json


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


EXPECTED = {
    TYPES_H: [
        "FAgrarianCalendarSnapshot",
        "AbsoluteDay",
        "FAgrarianGrowingSeasonProfile",
        "CropSafetyBufferDays",
        "FAgrarianCropSeasonAssessment",
        "bCanPlantToday",
    ],
    GAME_STATE_H: [
        "DaysPerAgrarianYear",
        "ActiveYear",
        "ActiveGrowingSeason",
        "ConvertAgrarianDaysToRealHours",
        "ConvertRealHoursToAgrarianDays",
        "GetLongTaskProgress",
        "IsDayInsideActiveGrowingSeason",
        "AssessCropForActiveGrowingSeason",
    ],
    GAME_STATE_CPP: [
        "DOREPLIFETIME(AAgrarianGameState, ActiveGrowingSeason);",
        "FAgrarianCalendarSnapshot AAgrarianGameState::GetCalendarSnapshot() const",
        "float AAgrarianGameState::ConvertAgrarianDaysToRealHours",
        "FAgrarianCropSeasonAssessment AAgrarianGameState::AssessCropForActiveGrowingSeason",
        "Crop maturity is too long for this tile's frost-free growing window.",
    ],
    GENERATOR: [
        "GROWING_READY_STATUSES",
        "GROWING_ZONE_OVERRIDES",
        "gz_us_ca_pacifica_utm10n_e544_n4160",
        "Only source-backed/generated tiles with explicit profiles are emitted.",
    ],
    SCHEMA: ["growing_season_metadata", "growing_zone_label", "frost_free_days"],
    SQL: ["terrain_tile_growing_season_metadata", "crop_safety_buffer_days"],
    ROADMAP: ["Add Agrarian calendar conversion helpers"],
}


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("Agrarian calendar verification failed: " + "; ".join(missing))


def assert_metadata() -> None:
    data = json.loads(METADATA.read_text(encoding="utf-8"))
    tiles = data.get("tiles", [])
    if len(tiles) != 1:
        raise RuntimeError(f"Expected exactly one growing-zone tile, got {len(tiles)}")
    tile = tiles[0]
    if tile.get("tile_id") != "gz_us_ca_pacifica_utm10n_e544_n4160":
        raise RuntimeError(f"Unexpected growing-zone tile id: {tile.get('tile_id')}")
    if tile.get("growing_zone_label") != "USDA 10a":
        raise RuntimeError("Ground Zero growing zone is missing or incorrect")
    if tile.get("frost_free_days", 0) < 250:
        raise RuntimeError(f"Ground Zero frost-free window is unexpectedly short: {tile}")


def main() -> None:
    assert_contains()
    assert_metadata()
    print("Agrarian calendar and growing-zone helper verification complete.")


if __name__ == "__main__":
    main()
