{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://agrarian.local/schemas/tile_registry.schema.json",
  "title": "Agrarian Terrain Tile Registry",
  "type": "object",
  "required": ["schema_version", "grid_scheme", "tiles"],
  "additionalProperties": false,
  "properties": {
    "schema_version": {
      "type": "integer",
      "minimum": 1
    },
    "grid_scheme": {
      "type": "string",
      "minLength": 1
    },
    "tiles": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/tile"
      }
    }
  },
  "$defs": {
    "tile_status": {
      "type": "string",
      "enum": [
        "unknown",
        "queued",
        "source_data_found",
        "generated",
        "validated",
        "packaged",
        "published",
        "deprecated",
        "blocked"
      ]
    },
    "tile": {
      "type": "object",
      "required": [
        "tile_id",
        "display_name",
        "grid",
        "status",
        "biome_primary",
        "generation_version",
        "package_version",
        "neighbors",
        "sources"
      ],
      "additionalProperties": false,
      "properties": {
        "tile_id": {
          "type": "string",
          "pattern": "^[a-z0-9_]+$"
        },
        "display_name": {
          "type": "string"
        },
        "grid": {
          "$ref": "#/$defs/grid"
        },
        "status": {
          "$ref": "#/$defs/tile_status"
        },
        "biome_primary": {
          "type": "string"
        },
        "biome_secondary": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "resource_hints": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "generation_version": {
          "type": "integer",
          "minimum": 0
        },
        "package_version": {
          "type": "integer",
          "minimum": 0
        },
        "neighbors": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "n": { "type": "string" },
            "ne": { "type": "string" },
            "e": { "type": "string" },
            "se": { "type": "string" },
            "s": { "type": "string" },
            "sw": { "type": "string" },
            "w": { "type": "string" },
            "nw": { "type": "string" }
          }
        },
        "sources": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/source"
          }
        },
        "solar_metadata": {
          "$ref": "#/$defs/solar_metadata"
        },
        "growing_season_metadata": {
          "$ref": "#/$defs/growing_season_metadata"
        },
        "weather_lookup_metadata": {
          "$ref": "#/$defs/weather_lookup_metadata"
        },
        "notes": {
          "type": "string"
        }
      }
    },
    "grid": {
      "type": "object",
      "required": [
        "projection",
        "utm_zone",
        "easting_min_m",
        "northing_min_m",
        "easting_max_m",
        "northing_max_m",
        "tile_size_m",
        "center_latitude",
        "center_longitude"
      ],
      "additionalProperties": false,
      "properties": {
        "projection": {
          "type": "string"
        },
        "utm_zone": {
          "type": "string"
        },
        "easting_min_m": {
          "type": "integer"
        },
        "northing_min_m": {
          "type": "integer"
        },
        "easting_max_m": {
          "type": "integer"
        },
        "northing_max_m": {
          "type": "integer"
        },
        "tile_size_m": {
          "type": "integer",
          "const": 1000
        },
        "center_latitude": {
          "type": "number",
          "minimum": -90,
          "maximum": 90
        },
        "center_longitude": {
          "type": "number",
          "minimum": -180,
          "maximum": 180
        }
      }
    },
    "solar_metadata": {
      "type": "object",
      "required": [
        "time_zone_id",
        "standard_utc_offset_hours",
        "daylight_utc_offset_hours",
        "solar_model"
      ],
      "additionalProperties": false,
      "properties": {
        "time_zone_id": {
          "type": "string"
        },
        "standard_utc_offset_hours": {
          "type": "number",
          "minimum": -12,
          "maximum": 14
        },
        "daylight_utc_offset_hours": {
          "type": "number",
          "minimum": -12,
          "maximum": 14
        },
        "solar_model": {
          "type": "string"
        }
      }
    },
    "growing_season_metadata": {
      "type": "object",
      "required": [
        "growing_zone_label",
        "climate_profile",
        "growing_season_start_day",
        "growing_season_end_day",
        "frost_free_days",
        "crop_safety_buffer_days"
      ],
      "additionalProperties": false,
      "properties": {
        "growing_zone_label": {
          "type": "string"
        },
        "climate_profile": {
          "type": "string"
        },
        "growing_season_start_day": {
          "type": "integer",
          "minimum": 1,
          "maximum": 366
        },
        "growing_season_end_day": {
          "type": "integer",
          "minimum": 1,
          "maximum": 366
        },
        "frost_free_days": {
          "type": "integer",
          "minimum": 0,
          "maximum": 366
        },
        "min_average_growing_temp_c": {
          "type": "number"
        },
        "crop_safety_buffer_days": {
          "type": "integer",
          "minimum": 0,
          "maximum": 90
        },
        "data_basis": {
          "type": "string"
        }
      }
    },
    "weather_lookup_metadata": {
      "type": "object",
      "required": [
        "lookup_latitude",
        "lookup_longitude",
        "coordinate_source",
        "primary_provider",
        "fallback_provider_eligible",
        "lookup_rule"
      ],
      "additionalProperties": false,
      "properties": {
        "lookup_latitude": {
          "type": "number",
          "minimum": -90,
          "maximum": 90
        },
        "lookup_longitude": {
          "type": "number",
          "minimum": -180,
          "maximum": 180
        },
        "coordinate_source": {
          "type": "string",
          "enum": ["tile_center", "manual_override", "provider_grid_point"]
        },
        "primary_provider": {
          "type": "string"
        },
        "fallback_provider": {
          "type": "string"
        },
        "fallback_provider_eligible": {
          "type": "boolean"
        },
        "lookup_rule": {
          "type": "string"
        }
      }
    },
    "source": {
      "type": "object",
      "required": ["source_kind", "source_name", "coverage_status"],
      "additionalProperties": false,
      "properties": {
        "source_kind": {
          "type": "string"
        },
        "source_name": {
          "type": "string"
        },
        "source_uri": {
          "type": "string"
        },
        "license_name": {
          "type": "string"
        },
        "source_version": {
          "type": "string"
        },
        "coverage_status": {
          "type": "string",
          "enum": ["needed", "candidate", "confirmed", "missing", "not_applicable"]
        }
      }
    }
  }
}
