{
  "$id": "/schemas/webhook-event-v1.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "event_version": {
      "type": "number",
      "const": 1,
      "description": "Event contract version."
    },
    "event_type": {
      "type": "string",
      "const": "batch.state_changed",
      "description": "Webhook event type."
    },
    "event_id": {
      "type": "string",
      "format": "uuid",
      "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$",
      "description": "Stable unique event identifier."
    },
    "occurred_at": {
      "type": "string",
      "format": "date-time",
      "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d(?:\\.\\d+)?)?(?:Z))$",
      "description": "ISO-8601 timestamp of the underlying provider state transition."
    },
    "watch_id": {
      "type": "string",
      "format": "uuid",
      "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$",
      "description": "Afterbatch watch identifier."
    },
    "project_id": {
      "type": "string",
      "format": "uuid",
      "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$",
      "description": "Afterbatch project identifier."
    },
    "environment": {
      "type": "string",
      "minLength": 1,
      "description": "Project environment where this watch is configured."
    },
    "batch_id": {
      "type": "string",
      "minLength": 1,
      "description": "Provider batch identifier."
    },
    "provider": {
      "type": "string",
      "enum": [
        "anthropic",
        "openai",
        "gemini"
      ],
      "description": "Provider that owns the batch."
    },
    "current_state": {
      "type": "string",
      "enum": [
        "pending",
        "in_progress",
        "completed",
        "failed",
        "canceled"
      ],
      "description": "Current canonical batch state."
    },
    "previous_state": {
      "anyOf": [
        {
          "type": "string",
          "enum": [
            "pending",
            "in_progress",
            "completed",
            "failed",
            "canceled"
          ]
        },
        {
          "type": "null"
        }
      ],
      "description": "Previous canonical state. Null when state history is unavailable."
    },
    "raw_status": {
      "type": "string",
      "minLength": 1,
      "description": "Provider-native status string observed at transition time."
    },
    "request_counts": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "total": {
              "type": "integer",
              "minimum": 0,
              "maximum": 9007199254740991,
              "description": "Total requests in the provider batch when available."
            },
            "succeeded": {
              "type": "integer",
              "minimum": 0,
              "maximum": 9007199254740991,
              "description": "Requests that finished successfully."
            },
            "failed": {
              "type": "integer",
              "minimum": 0,
              "maximum": 9007199254740991,
              "description": "Requests that reached a failed/canceled/expired outcome."
            }
          },
          "required": [
            "total",
            "succeeded",
            "failed"
          ],
          "additionalProperties": false
        },
        {
          "type": "null"
        }
      ],
      "description": "Provider-derived request counters when available, otherwise null."
    },
    "delivery_mode": {
      "type": "string",
      "enum": [
        "notification_only",
        "include_completed_data"
      ],
      "description": "Webhook delivery mode configured on the endpoint."
    },
    "completion_data": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "content_type": {
              "type": "string",
              "minLength": 1,
              "description": "Provider response content type."
            },
            "size_bytes": {
              "type": "integer",
              "minimum": 0,
              "maximum": 9007199254740991,
              "description": "Raw byte size of provider completion output."
            },
            "body": {
              "type": "string",
              "description": "Provider completion output body as UTF-8 text."
            }
          },
          "required": [
            "content_type",
            "size_bytes",
            "body"
          ],
          "additionalProperties": false
        },
        {
          "type": "null"
        }
      ],
      "description": "Completed batch output when delivery mode is include_completed_data; otherwise null."
    }
  },
  "required": [
    "event_version",
    "event_type",
    "event_id",
    "occurred_at",
    "watch_id",
    "project_id",
    "environment",
    "batch_id",
    "provider",
    "current_state",
    "previous_state",
    "raw_status",
    "request_counts",
    "delivery_mode",
    "completion_data"
  ],
  "additionalProperties": false,
  "description": "afterbatch webhook event payload v1"
}
