{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://github.com/andrewhuot/headless-chat-sdk/schemas/product-analytics.schema.json",
  "title": "ProductAnalyticsEvent",
  "description": "Discriminated union of product analytics journey events. Mirrors packages/gecx-chat/src/analytics/productAnalytics.ts.",
  "definitions": {
    "Base": {
      "type": "object",
      "required": ["eventId", "type", "sessionId", "timestamp"],
      "properties": {
        "eventId": { "type": "string" },
        "type": { "type": "string" },
        "sessionId": { "type": "string" },
        "timestamp": { "type": "string", "format": "date-time" },
        "context": { "type": "object" },
        "dimensions": { "type": "object" },
        "metadata": { "type": "object" }
      }
    },
    "PurchaseLineItem": {
      "type": "object",
      "required": ["productId", "name", "quantity", "unitPrice"],
      "properties": {
        "productId": { "type": "string" },
        "name": { "type": "string" },
        "quantity": { "type": "number", "minimum": 0 },
        "unitPrice": { "type": "number", "minimum": 0 },
        "category": { "type": "string" }
      }
    }
  },
  "oneOf": [
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "messageId", "role", "status", "turnIndex", "partTypes", "suggestionChipCount"],
          "properties": {
            "type": { "const": "message_impression" },
            "messageId": { "type": "string" },
            "role": { "type": "string", "enum": ["user", "agent"] },
            "status": { "type": "string", "enum": ["pending", "streaming", "completed", "error"] },
            "turnIndex": { "type": "integer" },
            "responseId": { "type": "string" },
            "partTypes": { "type": "array", "items": { "type": "string" } },
            "suggestionChipCount": { "type": "integer", "minimum": 0 }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "toolName", "status"],
          "properties": {
            "type": { "const": "server_tool_status" },
            "toolName": { "type": "string" },
            "toolCallId": { "type": "string" },
            "turnIndex": { "type": "integer" },
            "status": { "type": "string", "enum": ["requested", "approved", "pending", "denied", "duplicate", "completed", "failed", "timed_out"] },
            "approvalPolicy": { "type": "string", "enum": ["auto", "user_confirm", "supervisor_approve", "denied", "async_pending"] },
            "idempotencyKey": { "type": "string" },
            "duplicateDisposition": { "type": "string", "enum": ["replayed", "rejected", "conflict"] }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "messageId", "partId", "chipIndex"],
          "properties": {
            "type": { "const": "suggestion_chip_clicked" },
            "messageId": { "type": "string" },
            "partId": { "type": "string" },
            "chipIndex": { "type": "integer", "minimum": 0 },
            "turnIndex": { "type": "integer" },
            "responseId": { "type": "string" },
            "chipLabel": { "type": "string" },
            "chipValue": { "type": "string" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "toolName"],
          "properties": {
            "type": { "const": "tool_approval_requested" },
            "toolName": { "type": "string" },
            "toolCallId": { "type": "string" },
            "turnIndex": { "type": "integer" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "toolName", "approved"],
          "properties": {
            "type": { "const": "tool_approval_resolved" },
            "toolName": { "type": "string" },
            "toolCallId": { "type": "string" },
            "turnIndex": { "type": "integer" },
            "approved": { "type": "boolean" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "status"],
          "properties": {
            "type": { "enum": ["handoff_requested", "handoff_converted", "handoff_completed"] },
            "status": { "type": "string" },
            "turnIndex": { "type": "integer" },
            "responseId": { "type": "string" },
            "targetAgent": { "type": "string" },
            "reason": { "type": "string" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "score", "maxScore"],
          "properties": {
            "type": { "const": "csat_submitted" },
            "score": { "type": "number" },
            "maxScore": { "type": "number" },
            "comment": { "type": "string" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "resolved", "escalated", "withoutEscalation"],
          "properties": {
            "type": { "const": "resolution_submitted" },
            "resolved": { "type": "boolean" },
            "escalated": { "type": "boolean" },
            "withoutEscalation": { "type": "boolean" },
            "reason": { "type": "string" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "resolved"],
          "properties": {
            "type": { "const": "resolution_without_escalation" },
            "resolved": { "const": true }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "resumed", "identityKind"],
          "properties": {
            "type": { "const": "session_started" },
            "resumed": { "type": "boolean" },
            "identityKind": { "type": "string", "enum": ["guest", "authenticated"] },
            "conversationId": { "type": "string" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type"],
          "properties": {
            "type": { "const": "session_resumed" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type"],
          "properties": {
            "type": { "const": "session_idle" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type"],
          "properties": {
            "type": { "const": "session_abandoned" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "durationMs", "userTurnCount", "assistantTurnCount"],
          "properties": {
            "type": { "const": "session_ended" },
            "durationMs": { "type": "number" },
            "userTurnCount": { "type": "integer" },
            "assistantTurnCount": { "type": "integer" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "turnIndex", "charCount", "partTypes", "attachmentCount"],
          "properties": {
            "type": { "const": "user_message_sent" },
            "turnIndex": { "type": "integer" },
            "charCount": { "type": "integer" },
            "partTypes": { "type": "array", "items": { "type": "string" } },
            "attachmentCount": { "type": "integer" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "turnIndex", "responseId"],
          "properties": {
            "type": { "const": "assistant_response_started" },
            "turnIndex": { "type": "integer" },
            "responseId": { "type": "string" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "turnIndex", "responseId", "ttftMs"],
          "properties": {
            "type": { "const": "assistant_response_first_token" },
            "turnIndex": { "type": "integer" },
            "responseId": { "type": "string" },
            "ttftMs": { "type": "number" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "turnIndex", "responseId", "durationMs", "partTypeCounts", "finishReason"],
          "properties": {
            "type": { "const": "assistant_response_completed" },
            "turnIndex": { "type": "integer" },
            "responseId": { "type": "string" },
            "durationMs": { "type": "number" },
            "partTypeCounts": { "type": "object", "additionalProperties": { "type": "integer" } },
            "finishReason": { "type": "string", "enum": ["natural", "stopped", "errored", "aborted"] }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "turnIndex"],
          "properties": {
            "type": { "const": "stream_stopped" },
            "turnIndex": { "type": "integer" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "turnIndex"],
          "properties": {
            "type": { "const": "regenerate_requested" },
            "turnIndex": { "type": "integer" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "willRetry"],
          "properties": {
            "type": { "const": "transport_disconnected" },
            "willRetry": { "type": "boolean" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "outageMs"],
          "properties": {
            "type": { "const": "transport_reconnected" },
            "outageMs": { "type": "number" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "errorCode", "severity"],
          "properties": {
            "type": { "const": "error_shown" },
            "errorCode": { "type": "string" },
            "severity": { "type": "string", "enum": ["low", "medium", "high", "critical"] }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "errorCode"],
          "properties": {
            "type": { "const": "error_retry_clicked" },
            "errorCode": { "type": "string" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "messageId", "partId", "citationIndex"],
          "properties": {
            "type": { "const": "citation_clicked" },
            "messageId": { "type": "string" },
            "partId": { "type": "string" },
            "citationIndex": { "type": "integer" },
            "uri": { "type": "string" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "messageId", "partId", "productIndex"],
          "properties": {
            "type": { "const": "product_card_clicked" },
            "messageId": { "type": "string" },
            "partId": { "type": "string" },
            "productIndex": { "type": "integer" },
            "productSku": { "type": "string" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type"],
          "properties": {
            "type": { "const": "order_summary_action_clicked" },
            "action": { "type": "string" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "payloadType"],
          "properties": {
            "type": { "const": "custom_payload_interacted" },
            "payloadType": { "type": "string" },
            "action": { "type": "string" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "surfaceId", "actionName"],
          "properties": {
            "type": { "const": "a2ui_action_dispatched" },
            "surfaceId": { "type": "string" },
            "actionName": { "type": "string" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "componentId", "version", "integrity"],
          "properties": {
            "type": { "const": "a2ui_catalog_install" },
            "componentId": { "type": "string", "pattern": "^ui:[a-z][a-z0-9-]+$" },
            "version": { "type": "string" },
            "integrity": { "type": "string" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "componentId", "surfaceId", "version"],
          "properties": {
            "type": { "const": "a2ui_catalog_render" },
            "componentId": { "type": "string", "pattern": "^ui:[a-z][a-z0-9-]+$" },
            "surfaceId": { "type": "string" },
            "version": { "type": "string" },
            "durationMs": { "type": "number" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "componentId", "surfaceId", "actionName", "decision"],
          "properties": {
            "type": { "const": "a2ui_catalog_action" },
            "componentId": { "type": "string", "pattern": "^ui:[a-z][a-z0-9-]+$" },
            "surfaceId": { "type": "string" },
            "actionName": { "type": "string" },
            "decision": { "enum": ["allow", "confirm", "block"] }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "toolName", "toolCallId", "durationMs", "success"],
          "properties": {
            "type": { "const": "tool_executed" },
            "toolName": { "type": "string" },
            "toolCallId": { "type": "string" },
            "durationMs": { "type": "number" },
            "success": { "type": "boolean" },
            "errorCode": { "type": "string" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "kind", "bytes"],
          "properties": {
            "type": { "const": "file_upload_started" },
            "kind": { "type": "string" },
            "bytes": { "type": "number" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "kind", "bytes", "durationMs"],
          "properties": {
            "type": { "const": "file_upload_completed" },
            "kind": { "type": "string" },
            "bytes": { "type": "number" },
            "durationMs": { "type": "number" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "kind", "errorCode"],
          "properties": {
            "type": { "const": "file_upload_failed" },
            "kind": { "type": "string" },
            "errorCode": { "type": "string" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "entry", "turnId"],
          "properties": {
            "type": { "const": "agent_graph_entered" },
            "entry": { "type": "string" },
            "turnId": { "type": "string" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "turnId", "from", "to", "latencyMs"],
          "properties": {
            "type": { "const": "agent_routed" },
            "turnId": { "type": "string" },
            "from": { "type": "string" },
            "to": { "type": "string" },
            "latencyMs": { "type": "number", "minimum": 0 },
            "reason": { "type": "string" },
            "ruleIndex": { "type": ["integer", "null"] },
            "intentLabel": { "type": "string" },
            "intentConfidence": { "type": "number", "minimum": 0, "maximum": 1 }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "turnId", "specialistId"],
          "properties": {
            "type": { "const": "agent_specialist_started" },
            "turnId": { "type": "string" },
            "specialistId": { "type": "string" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "turnId", "specialistId", "durationMs"],
          "properties": {
            "type": { "const": "agent_specialist_completed" },
            "turnId": { "type": "string" },
            "specialistId": { "type": "string" },
            "durationMs": { "type": "number", "minimum": 0 }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "turnId", "specialistId", "durationMs", "errorCode"],
          "properties": {
            "type": { "const": "agent_specialist_failed" },
            "turnId": { "type": "string" },
            "specialistId": { "type": "string" },
            "durationMs": { "type": "number", "minimum": 0 },
            "errorCode": { "type": "string" }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "turnId", "finalNode", "totalLatencyMs"],
          "properties": {
            "type": { "const": "agent_graph_exited" },
            "turnId": { "type": "string" },
            "finalNode": { "type": "string" },
            "totalLatencyMs": { "type": "number", "minimum": 0 }
          }
        }
      ]
    },
    {
      "allOf": [
        { "$ref": "#/definitions/Base" },
        {
          "type": "object",
          "required": ["type", "orderId", "revenue", "currency"],
          "properties": {
            "type": { "const": "purchase_completed" },
            "orderId": { "type": "string" },
            "revenue": { "type": "number", "minimum": 0 },
            "currency": { "type": "string", "pattern": "^[A-Z]{3}$" },
            "items": {
              "type": "array",
              "items": { "$ref": "#/definitions/PurchaseLineItem" }
            },
            "paymentMethod": { "type": "string" }
          }
        }
      ]
    }
  ]
}
