{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://qazpolit.com/contracts/qazpipe-evidence-v2.schema.json",
  "title": "QazPipe to Qazpolit evidence snapshot",
  "type": "object",
  "required": ["schema", "generated_at"],
  "properties": {
    "schema": {"const": "qazpipe.qazpolit.evidence.v2"},
    "generated_at": {"type": "string", "format": "date-time"},
    "sources": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["url", "title"],
        "properties": {
          "url": {"type": "string", "format": "uri"},
          "title": {"type": "string", "minLength": 1, "maxLength": 500},
          "publisher": {"type": ["string", "null"], "maxLength": 255},
          "source_type": {"type": "string", "maxLength": 50},
          "language": {"type": ["string", "null"], "maxLength": 5},
          "published_date": {"type": ["string", "null"], "maxLength": 50},
          "rights_status": {"type": "string", "maxLength": 30},
          "snapshots": {
            "type": "array",
            "items": {
              "type": "object",
              "required": ["snapshot_key", "fetched_url"],
              "properties": {
                "snapshot_key": {"type": "string", "minLength": 1, "maxLength": 255},
                "fetched_url": {"type": "string", "format": "uri"},
                "archived_url": {"type": ["string", "null"], "format": "uri"},
                "fetched_at": {"type": ["string", "null"], "format": "date-time"},
                "status_code": {"type": ["integer", "null"], "minimum": 100, "maximum": 599},
                "mime_type": {"type": ["string", "null"], "maxLength": 255},
                "content_checksum": {"type": ["string", "null"], "maxLength": 128},
                "storage_uri": {"type": ["string", "null"]},
                "collector": {"type": ["string", "null"], "maxLength": 100}
              },
              "additionalProperties": false
            }
          }
        },
        "additionalProperties": false
      }
    },
    "identifiers": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["entity_slug", "scheme", "value"],
        "properties": {
          "entity_slug": {"type": "string", "maxLength": 255},
          "scheme": {"type": "string", "maxLength": 100},
          "value": {"type": "string", "maxLength": 500},
          "source_url": {"type": ["string", "null"], "format": "uri"},
          "review_status": {"enum": ["draft", "in_review", "reviewed", "published"]},
          "is_primary": {"type": "boolean"}
        },
        "additionalProperties": false
      }
    },
    "media_items": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["item_key", "canonical_url", "title", "matches"],
        "properties": {
          "item_key": {"type": "string", "maxLength": 255},
          "canonical_url": {"type": "string", "format": "uri"},
          "title": {"type": "string", "maxLength": 1000},
          "source_system": {"type": "string", "maxLength": 80},
          "platform": {"type": "string", "maxLength": 80},
          "media_type": {"type": "string", "maxLength": 50},
          "publisher": {"type": ["string", "null"], "maxLength": 255},
          "author": {"type": ["string", "null"], "maxLength": 255},
          "language": {"type": ["string", "null"], "maxLength": 10},
          "published_at": {"type": ["string", "null"], "format": "date-time"},
          "content_hash": {"type": ["string", "null"], "maxLength": 128},
          "collected_at": {"type": ["string", "null"], "format": "date-time"},
          "matches": {
            "type": "array",
            "items": {
              "type": "object",
              "required": ["entity_slug"],
              "properties": {
                "entity_slug": {"type": "string", "maxLength": 255},
                "excerpt": {"type": ["string", "null"]},
                "start_offset": {"type": ["integer", "null"], "minimum": 0},
                "end_offset": {"type": ["integer", "null"], "minimum": 0},
                "matched_alias": {"type": ["string", "null"], "maxLength": 500},
                "sentiment": {"type": ["number", "null"], "minimum": -1, "maximum": 1},
                "reach": {"type": ["integer", "null"], "minimum": 0},
                "match_confidence": {"type": ["number", "null"], "minimum": 0, "maximum": 1},
                "review_status": {"enum": ["pending", "approved", "rejected", "published"]}
              },
              "additionalProperties": false
            }
          }
        },
        "additionalProperties": false
      }
    },
    "relationship_evidence": {"type": "array", "items": {"$ref": "#/$defs/evidenceLink"}},
    "event_evidence": {"type": "array", "items": {"$ref": "#/$defs/eventEvidence"}},
    "statement_evidence": {"type": "array", "items": {"$ref": "#/$defs/statementEvidence"}},
    "documents": {"type": "array", "items": {"$ref": "#/$defs/document"}}
  },
  "$defs": {
    "evidence": {
      "type": "object",
      "required": ["source_url"],
      "properties": {
        "source_url": {"type": "string", "format": "uri"},
        "snapshot_key": {"type": ["string", "null"], "maxLength": 255},
        "stance": {"enum": ["supports", "disputes", "context"]},
        "locator": {"type": ["string", "null"], "maxLength": 255},
        "excerpt": {"type": ["string", "null"]},
        "verification_status": {"enum": ["unreviewed", "in_review", "reviewed", "published"]},
        "confidence": {"type": ["number", "null"], "minimum": 0, "maximum": 1}
      }
    },
    "evidenceLink": {
      "allOf": [
        {"$ref": "#/$defs/evidence"},
        {
          "type": "object",
          "required": ["source_entity_slug", "target_entity_slug", "link_type"],
          "properties": {
            "source_entity_slug": {"type": "string", "maxLength": 255},
            "target_entity_slug": {"type": "string", "maxLength": 255},
            "link_type": {"type": "string", "maxLength": 50}
          }
        }
      ]
    },
    "eventEvidence": {
      "allOf": [
        {"$ref": "#/$defs/evidence"},
        {
          "type": "object",
          "required": ["entity_slug", "event_title"],
          "properties": {
            "entity_slug": {"type": "string", "maxLength": 255},
            "event_title": {"type": "string", "maxLength": 500},
            "event_date": {"type": ["string", "null"], "maxLength": 50},
            "lang": {"type": "string", "maxLength": 5}
          }
        }
      ]
    },
    "statementEvidence": {
      "allOf": [
        {"$ref": "#/$defs/evidence"},
        {
          "type": "object",
          "required": ["entity_slug", "property_key", "value"],
          "properties": {
            "entity_slug": {"type": "string", "maxLength": 255},
            "property_key": {"type": "string", "maxLength": 100},
            "value": {"type": "string", "minLength": 1},
            "lang": {"type": "string", "maxLength": 5}
          }
        }
      ]
    },
    "document": {
      "type": "object",
      "required": ["document_slug", "blocks"],
      "properties": {
        "document_slug": {"type": "string", "maxLength": 255},
        "blocks": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["block_index", "text"],
            "properties": {
              "block_index": {"type": "integer", "minimum": 0},
              "page_number": {"type": ["integer", "null"], "minimum": 1},
              "block_type": {"type": "string", "maxLength": 50},
              "text": {"type": "string"},
              "locator": {"type": ["string", "null"], "maxLength": 255},
              "content_hash": {"type": ["string", "null"], "maxLength": 128},
              "matches": {"type": "array", "items": {"$ref": "#/$defs/documentMatch"}}
            },
            "additionalProperties": false
          }
        }
      },
      "additionalProperties": false
    },
    "documentMatch": {
      "type": "object",
      "required": ["entity_slug"],
      "properties": {
        "entity_slug": {"type": "string", "maxLength": 255},
        "excerpt": {"type": ["string", "null"]},
        "start_offset": {"type": ["integer", "null"], "minimum": 0},
        "end_offset": {"type": ["integer", "null"], "minimum": 0},
        "matched_alias": {"type": ["string", "null"], "maxLength": 500},
        "match_confidence": {"type": ["number", "null"], "minimum": 0, "maximum": 1},
        "review_status": {"enum": ["pending", "approved", "rejected", "published"]}
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false
}
