{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://contenttelemetry.org/schema/v1/telemetry-event-batch.json",
  "title": "Content Telemetry Event Batch",
  "description": "Schema for batches of Content Telemetry events sharing one session context, delivered outside a session document",
  "type": "object",
  "required": ["document_type", "schema_version", "events"],
  "properties": {
    "document_type": {
      "type": "string",
      "const": "event_batch",
      "description": "Document type discriminator. MUST be 'event_batch' for event batches."
    },
    "schema_version": {
      "type": "string",
      "const": "1.0",
      "description": "Content Telemetry schema version"
    },
    "session_id": {
      "type": "string",
      "format": "uuid",
      "description": "Session identifier, applying to every event in the batch. MAY be omitted by origin-side emitters with no session context. REQUIRED for emitters at Grounding conformance or above (unless ctx_token is carried instead - see section 7.1). Events belonging to different sessions MUST be delivered in separate batches."
    },
    "parent_session_id": {
      "type": "string",
      "format": "uuid",
      "description": "Optional identifier of the immediate parent session that delegated work to this session. Applies to every event in the batch. See section 5.1."
    },
    "ctx_token": {
      "type": "string",
      "pattern": "^ct_[A-Za-z0-9_-]{16,240}$",
      "description": "Opaque click token issued by the originating agent, carried in place of session_id on batches of content_engaged events emitted from a landing page after a click-out. Applies to every event in the batch and is resolved by the telemetry consumer to the click context (section 7.4). An event MUST carry either session_id or ctx_token at Grounding conformance and above."
    },
    "agent_id": {
      "type": ["string", "null"],
      "description": "Responding agent identifier. REQUIRED for emitters at Grounding conformance or above when using event batch delivery. Mirrors the session-level agent_id field."
    },
    "started_at": {
      "type": "string",
      "format": "date-time",
      "description": "Session start timestamp (UTC). Mirrors the session-level started_at field. REQUIRED for emitters at Grounding conformance or above when using event batch delivery."
    },
    "manifest_ref": {
      "type": ["string", "null"],
      "format": "uri",
      "description": "Manifest reference - the URL of a manifest at /.well-known/content-telemetry.json identifying the emitter. Applies to every event in the batch and mirrors the session-level manifest_ref field. See sections 7.1 and 8."
    },
    "events": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "telemetry-session.json#/$defs/TelemetryEvent"
      },
      "description": "The telemetry events in the batch"
    }
  },
  "allOf": [
    {
      "if": {
        "not": { "required": ["ctx_token"] }
      },
      "then": {
        "properties": {
          "events": {
            "items": {
              "if": {
                "properties": { "type": { "const": "content_engaged" } },
                "required": ["type"]
              },
              "then": { "required": ["presentation_id"] }
            }
          }
        }
      }
    }
  ]
}
