{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://pointcast.xyz/observatory/report.schema.json",
  "title": "PointCast Observatory on-demand report v1",
  "type": "object",
  "required": ["schema_version", "topic", "location", "local_date", "generated_at", "status", "metrics", "sources", "errors"],
  "properties": {
    "schema_version": {"const": "pointcast.observatory.report.v1"},
    "topic": {"enum": ["moon", "sun", "pacific", "air"]},
    "location": {"type": "object", "required": ["name", "latitude", "longitude", "time_zone"], "properties": {"name": {"type": "string"}, "latitude": {"type": "number"}, "longitude": {"type": "number"}, "time_zone": {"const": "America/Los_Angeles"}}},
    "local_date": {"type": "string", "pattern": "^\\d{4}-\\d{2}-\\d{2}$"},
    "generated_at": {"type": "string", "format": "date-time", "description": "Report assembly time, not source observation time."},
    "status": {"enum": ["available", "partial", "stale", "unavailable"]},
    "metrics": {"type": "array", "items": {"$ref": "#/$defs/metric"}},
    "sources": {"type": "object", "additionalProperties": {"type": "object", "required": ["label", "documentation", "authentication", "request_url", "retrieved_at", "status"]}},
    "errors": {"type": "array", "items": {"type": "object", "required": ["source", "code"], "properties": {"source": {"type": "string"}, "code": {"type": "string"}}}},
    "notes": {"type": "array", "items": {"type": "string"}}
  },
  "$defs": {
    "metric": {
      "type": "object",
      "required": ["id", "label", "value", "unit", "kind", "status", "source", "location", "valid_at", "valid_date", "age_seconds", "max_age_seconds", "note", "quality", "station_id", "coordinates", "reason"],
      "properties": {
        "id": {"type": "string"}, "label": {"type": "string"},
        "value": {"type": ["number", "string", "null"]}, "unit": {"type": ["string", "null"]},
        "kind": {"enum": ["astronomical_calculation", "observation", "estimate", "external_reference"]},
        "status": {"enum": ["available", "stale", "unavailable", "not_applicable"]},
        "source": {"type": "string", "description": "Key in the report sources ledger."}, "location": {"type": "string"},
        "valid_at": {"type": ["string", "null"], "format": "date-time"}, "valid_date": {"type": ["string", "null"]},
        "age_seconds": {"type": ["number", "null"], "minimum": 0}, "max_age_seconds": {"type": ["number", "null"], "minimum": 0},
        "note": {"type": ["string", "null"]}, "quality": {"description": "Source quality metadata preserved without inventing interpretation."},
        "station_id": {"type": ["string", "null"]}, "coordinates": {"type": ["object", "null"]}, "reason": {"type": ["string", "null"]}
      },
      "allOf": [{"if": {"properties": {"status": {"enum": ["unavailable", "not_applicable"]}}}, "then": {"properties": {"value": {"type": "null"}}}}]
    }
  }
}
