{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://aiwg.io/schemas/civic-action/v1/local-resource-index.schema.json",
  "title": "Local public resource index record",
  "type": "object", "additionalProperties": false,
  "required": ["schema", "record_id", "vertical", "source_id", "retrieval_id", "publisher_verified", "profile", "payload_hash", "freshness_state", "public_scope", "vertical_fields", "facts", "citations", "structured_data", "correction_contact", "review", "publication_gate_status", "validation"],
  "properties": {
    "schema": {"const": "aiwg.civic.local-resource-index.v1"},
    "record_id": {"type": "string", "minLength": 1},
    "vertical": {"enum": ["cap", "gtfs", "hsds"]},
    "source_id": {"type": "string", "minLength": 1},
    "retrieval_id": {"type": "string", "minLength": 1},
    "publisher_verified": {"type": "boolean"},
    "profile": {"type": "string", "minLength": 1},
    "payload_hash": {"type": "string", "pattern": "^sha256:[a-f0-9]{64}$"},
    "freshness_state": {"enum": ["fresh", "due", "stale", "expired", "unknown", "unavailable"]},
    "public_scope": {"type": "boolean"},
    "vertical_fields": {"type": "object", "additionalProperties": false, "properties": {"identifier": {"type": "string", "minLength": 1}, "sender": {"type": "string", "minLength": 1}, "sent": {"type": "string", "format": "date-time"}, "status": {"enum": ["Actual", "Exercise", "System", "Test", "Draft"]}, "msg_type": {"enum": ["Alert", "Update", "Cancel", "Ack", "Error"]}, "scope": {"enum": ["Public", "Restricted", "Private"]}, "references": {"type": "array", "items": {"type": "string"}}, "incidents": {"type": "array", "items": {"type": "string"}}, "language": {"type": "string", "minLength": 1}, "category": {"type": "array", "minItems": 1, "items": {"type": "string"}}, "event": {"type": "string", "minLength": 1}, "urgency": {"type": "string", "minLength": 1}, "severity": {"type": "string", "minLength": 1}, "certainty": {"type": "string", "minLength": 1}, "effective": {"type": ["string", "null"], "format": "date-time"}, "onset": {"type": ["string", "null"], "format": "date-time"}, "expires": {"type": ["string", "null"], "format": "date-time"}, "headline": {"type": "string", "minLength": 1}, "description": {"type": "string", "minLength": 1}, "instruction": {"type": ["string", "null"]}, "web": {"type": ["string", "null"], "format": "uri"}, "contact": {"type": ["string", "null"]}, "areas": {"type": "array", "minItems": 1, "items": {"type": "string"}}, "feed_version": {"type": "string", "minLength": 1}, "agency_ids": {"type": "array", "minItems": 1, "items": {"type": "string"}}, "route_ids": {"type": "array", "items": {"type": "string"}}, "trip_ids": {"type": "array", "items": {"type": "string"}}, "stop_ids": {"type": "array", "items": {"type": "string"}}, "stop_time_count": {"type": "integer", "minimum": 0}, "service_ids": {"type": "array", "items": {"type": "string"}}, "calendar_dates": {"type": "array", "items": {"type": "string", "format": "date"}}, "agency_timezone": {"type": "string", "minLength": 1}, "service_date": {"type": "string", "format": "date"}, "active_service_dates": {"type": "array", "minItems": 1, "items": {"type": "string", "format": "date"}}, "organization_id": {"type": "string", "minLength": 1}, "service_id": {"type": "string", "minLength": 1}, "location_ids": {"type": "array", "items": {"type": "string"}}, "service_at_location_ids": {"type": "array", "items": {"type": "string"}}, "schedule_ids": {"type": "array", "items": {"type": "string"}}, "contact_ids": {"type": "array", "items": {"type": "string"}}, "phone_ids": {"type": "array", "items": {"type": "string"}}, "eligibility": {"type": ["string", "null"]}, "fees": {"type": ["string", "null"]}, "languages": {"type": "array", "items": {"type": "string"}}, "taxonomy_terms": {"type": "array", "minItems": 1, "items": {"type": "string", "minLength": 1}}, "service_areas": {"type": "array", "items": {"type": "string"}}}},
    "facts": {"type": "object", "additionalProperties": true},
    "citations": {"type": "array", "minItems": 1, "items": {"type": "object", "additionalProperties": false, "required": ["source_id", "retrieval_id", "selector"], "properties": {"source_id": {"type": "string", "minLength": 1}, "retrieval_id": {"type": "string", "minLength": 1}, "selector": {"type": "string", "minLength": 1}}}},
    "structured_data": {"type": "object", "additionalProperties": false, "required": ["format", "mapping_state"], "properties": {"format": {"enum": ["cap-1.2", "gtfs-static", "hsds", "json-ld"]}, "mapping_state": {"enum": ["native", "documented_transform", "manual_review", "blocked"]}}},
    "correction_contact": {"type": "string", "format": "uri"},
    "review": {"type": "object", "additionalProperties": false, "required": ["correction_state", "takedown_state", "reviewer"], "properties": {"correction_state": {"enum": ["clear", "pending", "blocked"]}, "takedown_state": {"enum": ["clear", "requested", "approved", "rejected", "pending"]}, "reviewer": {"type": ["string", "null"]}}},
    "publication_gate_status": {"enum": ["pass", "warn", "block", "not_run"]},
    "validation": {"type": "object", "additionalProperties": false, "required": ["validator", "version", "status", "findings"], "properties": {"validator": {"type": "string"}, "version": {"type": "string"}, "status": {"enum": ["pass", "warn", "block"]}, "findings": {"type": "array", "items": {"type": "string"}}}}
  },
  "allOf": [
    {"if": {"properties": {"vertical": {"const": "cap"}}}, "then": {"properties": {"vertical_fields": {"required": ["identifier", "sender", "sent", "status", "msg_type", "scope", "references", "incidents", "language", "category", "event", "urgency", "severity", "certainty", "effective", "onset", "expires", "headline", "description", "instruction", "web", "contact", "areas"]}, "structured_data": {"properties": {"format": {"const": "cap-1.2"}}}}}},
    {"if": {"properties": {"vertical": {"const": "gtfs"}}}, "then": {"properties": {"vertical_fields": {"required": ["feed_version", "agency_ids", "route_ids", "trip_ids", "stop_ids", "stop_time_count", "service_ids", "calendar_dates", "agency_timezone", "service_date", "active_service_dates"]}, "structured_data": {"properties": {"format": {"const": "gtfs-static"}}}}}},
    {"if": {"properties": {"vertical": {"const": "hsds"}}}, "then": {"properties": {"vertical_fields": {"required": ["organization_id", "service_id", "location_ids", "service_at_location_ids", "schedule_ids", "contact_ids", "phone_ids", "eligibility", "fees", "languages", "taxonomy_terms", "service_areas"]}, "structured_data": {"properties": {"format": {"const": "hsds"}}}}}},
    {
      "if": {"properties": {"publication_gate_status": {"const": "pass"}}},
      "then": {"properties": {
        "publisher_verified": {"const": true},
        "freshness_state": {"const": "fresh"},
        "public_scope": {"const": true},
        "structured_data": {"properties": {"mapping_state": {"enum": ["native", "documented_transform"]}}},
        "review": {"properties": {
          "correction_state": {"const": "clear"},
          "takedown_state": {"enum": ["clear", "rejected"]},
          "reviewer": {"type": "string", "minLength": 1}
        }},
        "validation": {"properties": {"status": {"const": "pass"}}}
      }}
    },
    {
      "if": {"allOf": [
        {"properties": {"vertical": {"const": "cap"}}},
        {"properties": {"publication_gate_status": {"const": "pass"}}}
      ]},
      "then": {"properties": {"vertical_fields": {"properties": {
        "status": {"const": "Actual"},
        "scope": {"const": "Public"}
      }}}}
    }
  ]
}
