{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://oriann.app/docs/webhooks.schema.json",
  "title": "Oriann Developer Webhooks",
  "description": "Contrato publico vigente de webhooks salientes de Oriann.",
  "oneOf": [
    { "$ref": "#/$defs/ticketEvent" },
    { "$ref": "#/$defs/legacyResourceEvent" }
  ],
  "x-signature": {
    "headers": [
      "oriann-webhook-id",
      "oriann-webhook-timestamp",
      "oriann-webhook-signature",
      "oriann-webhook-api-version"
    ],
    "algorithm": "HMAC-SHA256",
    "signedPayload": "eventId.timestamp.rawBody",
    "timestampToleranceSeconds": 300
  },
  "x-delivery": {
    "semantics": "at-least-once",
    "ticketOrdering": "ordered_per_ticket_and_endpoint",
    "dedupeBy": "id",
    "successStatus": "Any 2xx response",
    "retryOn": ["network_error", "timeout", "429", "5xx"],
    "retriesPreserve": ["id", "rawBody"],
    "maxAttemptsPerDelivery": 5,
    "requestTimeoutSeconds": 5,
    "maxActiveTicketEndpointsPerAgent": 3,
    "endpointCircuitBreakerConsecutiveFailures": 3,
    "previousSecretValidityHours": 24,
    "testEventsUseLivemodeFalse": true
  },
  "$defs": {
    "ticketEvent": {
      "type": "object",
      "required": ["id", "type", "createdAt", "apiVersion", "livemode", "data"],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^evt_[0-9a-f]{32}$"
        },
        "type": {
          "type": "string",
          "enum": [
            "ticket.created",
            "ticket.updated",
            "ticket.resolved",
            "ticket.reopened",
            "ticket.deleted"
          ]
        },
        "createdAt": {
          "type": "string",
          "format": "date-time"
        },
        "apiVersion": {
          "type": "string",
          "const": "2026-07-26"
        },
        "livemode": {
          "type": "boolean"
        },
        "data": {
          "type": "object",
          "required": ["object"],
          "additionalProperties": false,
          "properties": {
            "object": {
              "oneOf": [
                { "$ref": "#/$defs/ticketSnapshot" },
                { "$ref": "#/$defs/ticketTombstone" }
              ]
            }
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": { "type": { "const": "ticket.deleted" } },
            "required": ["type"]
          },
          "then": {
            "properties": {
              "data": {
                "properties": {
                  "object": { "$ref": "#/$defs/ticketTombstone" }
                }
              }
            }
          },
          "else": {
            "properties": {
              "data": {
                "properties": {
                  "object": { "$ref": "#/$defs/ticketSnapshot" }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": { "type": { "const": "ticket.resolved" } },
            "required": ["type"]
          },
          "then": {
            "properties": {
              "data": {
                "properties": {
                  "object": {
                    "properties": {
                      "status": { "const": "resolved" }
                    }
                  }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": { "type": { "const": "ticket.reopened" } },
            "required": ["type"]
          },
          "then": {
            "properties": {
              "data": {
                "properties": {
                  "object": {
                    "properties": {
                      "status": { "const": "reopened" }
                    }
                  }
                }
              }
            }
          }
        }
      ]
    },
    "ticketSnapshot": {
      "type": "object",
      "required": [
        "object",
        "id",
        "agentId",
        "conversationId",
        "flowId",
        "status",
        "ticketVersion",
        "attributes",
        "outcome",
        "createdAt",
        "updatedAt",
        "resolvedAt"
      ],
      "additionalProperties": false,
      "properties": {
        "object": { "type": "string", "const": "ticket" },
        "id": { "type": "string" },
        "agentId": { "type": "string" },
        "conversationId": { "type": "string" },
        "leadId": { "type": "string" },
        "flowId": { "type": "string" },
        "status": {
          "type": "string",
          "enum": ["open", "resolved", "reopened"]
        },
        "ticketVersion": {
          "type": "integer",
          "minimum": 1
        },
        "attributes": {
          "type": "array",
          "items": { "$ref": "#/$defs/ticketAttribute" }
        },
        "outcome": {
          "oneOf": [{ "$ref": "#/$defs/ticketOutcome" }, { "type": "null" }]
        },
        "createdAt": { "type": "string", "format": "date-time" },
        "updatedAt": { "type": "string", "format": "date-time" },
        "resolvedAt": { "type": ["string", "null"], "format": "date-time" }
      }
    },
    "ticketAttribute": {
      "type": "object",
      "required": ["id", "type", "value", "queryable"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string" },
        "type": {
          "type": "string",
          "enum": [
            "string",
            "enum",
            "boolean",
            "number",
            "percentage",
            "date",
            "datetime"
          ]
        },
        "value": {
          "type": ["string", "number", "boolean", "null"]
        },
        "queryable": { "type": "boolean" }
      }
    },
    "ticketOutcome": {
      "type": "object",
      "required": ["code", "summary"],
      "additionalProperties": false,
      "properties": {
        "code": { "type": "string" },
        "summary": { "type": "string" }
      }
    },
    "ticketTombstone": {
      "type": "object",
      "required": ["object", "id", "deletedAt"],
      "additionalProperties": false,
      "properties": {
        "object": { "type": "string", "const": "ticket_tombstone" },
        "id": { "type": "string" },
        "deletedAt": { "type": "string", "format": "date-time" }
      }
    },
    "legacyResourceEvent": {
      "type": "object",
      "required": [
        "id",
        "type",
        "created_at",
        "api_version",
        "livemode",
        "data"
      ],
      "additionalProperties": true,
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^evt_[0-9a-f]{32}$"
        },
        "type": {
          "type": "string",
          "enum": [
            "lead.created",
            "lead.updated",
            "conversation.created",
            "conversation.status_updated",
            "message.created",
            "message.delivery_updated"
          ]
        },
        "created_at": {
          "type": "string",
          "format": "date-time"
        },
        "api_version": {
          "type": "string",
          "const": "2026-07-26"
        },
        "livemode": {
          "type": "boolean"
        },
        "data": {
          "type": "object",
          "required": ["object"],
          "additionalProperties": true,
          "properties": {
            "object": {
              "oneOf": [
                { "$ref": "#/$defs/lead" },
                { "$ref": "#/$defs/conversation" },
                { "$ref": "#/$defs/message" }
              ]
            }
          }
        }
      }
    },
    "lead": {
      "type": "object",
      "description": "Lead legacy shell with an optional immutable OAD projection.",
      "required": [
        "id",
        "agent_id",
        "conversation_id",
        "created_at",
        "updated_at"
      ],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string", "maxLength": 512 },
        "agent_id": { "type": "string", "maxLength": 512 },
        "conversation_id": { "type": "string", "maxLength": 512 },
        "channel": { "$ref": "#/$defs/channelNullable" },
        "status": { "type": "string", "maxLength": 64 },
        "qualification": { "type": ["string", "null"], "maxLength": 64 },
        "lead_score": { "type": ["number", "null"] },
        "version": { "type": ["integer", "null"], "minimum": 1 },
        "created_at": { "type": "string", "format": "date-time" },
        "updated_at": { "type": "string", "format": "date-time" },
        "lead_definition_id": { "type": "string", "maxLength": 512 },
        "oad_revision_id": {
          "type": ["string", "null"],
          "maxLength": 512
        },
        "oad_definition_digest": {
          "type": ["string", "null"],
          "maxLength": 512
        },
        "lifecycle_state_id": {
          "type": ["string", "null"],
          "maxLength": 512
        },
        "data": { "$ref": "#/$defs/leadDynamicObject" },
        "qualification_outputs": {
          "$ref": "#/$defs/leadDynamicObject"
        },
        "qualification_outputs_automatic": {
          "$ref": "#/$defs/leadDynamicObject"
        },
        "qualification_override": {
          "oneOf": [
            { "$ref": "#/$defs/leadQualificationOverride" },
            { "type": "null" }
          ]
        },
        "qualification_evaluation": {
          "$ref": "#/$defs/leadQualificationEvaluation"
        },
        "evidence_resolution": {
          "$ref": "#/$defs/leadEvidenceResolution"
        },
        "evidence_provenance": {
          "type": "array",
          "maxItems": 64,
          "items": { "$ref": "#/$defs/leadEvidenceProvenanceEntry" }
        },
        "evidence_provenance_truncated": {
          "type": "boolean",
          "description": "True when additional eligible evidence provenance entries were omitted at the public payload limit."
        }
      },
      "allOf": [
        {
          "if": { "required": ["lead_definition_id"] },
          "then": {
            "required": [
              "oad_revision_id",
              "oad_definition_digest",
              "lifecycle_state_id",
              "version"
            ]
          }
        }
      ]
    },
    "leadDynamicValue": {
      "oneOf": [
        { "type": "string", "maxLength": 8192 },
        { "type": "number" },
        { "type": "boolean" },
        { "type": "null" },
        {
          "type": "array",
          "items": { "$ref": "#/$defs/leadDynamicValue" }
        },
        { "$ref": "#/$defs/leadDynamicObject" }
      ]
    },
    "leadDynamicObject": {
      "type": "object",
      "description": "Dynamic Lead values are bounded by the runtime's 8 KiB serialized value limit; nested property count is intentionally not constrained by this schema.",
      "additionalProperties": {
        "$ref": "#/$defs/leadDynamicValue"
      }
    },
    "leadProvenance": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "conversationId": { "type": "string", "maxLength": 512 },
        "messageId": { "type": "string", "maxLength": 512 },
        "evidenceId": { "type": "string", "maxLength": 512 },
        "actionId": { "type": "string", "maxLength": 512 },
        "source": { "type": "string", "maxLength": 512 },
        "kind": { "type": "string", "maxLength": 512 },
        "observedAt": { "type": "number" },
        "originRuleIds": {
          "type": "array",
          "maxItems": 64,
          "items": { "type": "string", "maxLength": 512 }
        },
        "truncated": { "type": "boolean" }
      }
    },
    "leadQualificationOverride": {
      "type": "object",
      "additionalProperties": false,
      "required": ["values"],
      "properties": {
        "schemaVersion": { "type": "string", "maxLength": 512 },
        "actorType": { "type": "string", "enum": ["ai", "human", "system"] },
        "actorId": { "type": "string", "maxLength": 512 },
        "actionId": { "type": "string", "maxLength": 512 },
        "originRuleIds": {
          "type": "array",
          "maxItems": 64,
          "items": { "type": "string", "maxLength": 512 }
        },
        "oadRevisionId": { "type": "string", "maxLength": 512 },
        "oadDefinitionDigest": { "type": "string", "maxLength": 512 },
        "setAt": { "type": "number" },
        "values": { "$ref": "#/$defs/leadDynamicObject" },
        "evidence": { "$ref": "#/$defs/leadProvenance" }
      }
    },
    "leadQualificationOutput": {
      "type": "object",
      "additionalProperties": false,
      "required": ["outputId"],
      "properties": {
        "outputId": { "type": "string", "maxLength": 512 },
        "label": { "type": "string", "maxLength": 512 },
        "kind": { "type": "string", "maxLength": 64 },
        "inputFieldIds": {
          "type": "array",
          "maxItems": 32,
          "items": { "type": "string", "maxLength": 512 }
        },
        "refreshPolicy": { "type": "string", "maxLength": 64 },
        "status": {
          "type": "string",
          "enum": ["present", "omitted", "null", "defaulted", "deferred"]
        },
        "value": { "$ref": "#/$defs/leadDynamicValue" },
        "evidenceIds": {
          "type": "array",
          "maxItems": 32,
          "items": { "type": "string", "maxLength": 512 }
        }
      }
    },
    "leadQualificationEvaluation": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "method": {
          "type": "string",
          "enum": ["rules", "formula", "governed_evaluation"]
        },
        "evaluatorId": { "type": "string", "maxLength": 512 },
        "values": { "$ref": "#/$defs/leadDynamicObject" },
        "outputs": {
          "type": "array",
          "maxItems": 64,
          "items": { "$ref": "#/$defs/leadQualificationOutput" }
        },
        "missingOutputIds": {
          "type": "array",
          "maxItems": 64,
          "items": { "type": "string", "maxLength": 512 }
        },
        "deferredOutputIds": {
          "type": "array",
          "maxItems": 64,
          "items": { "type": "string", "maxLength": 512 }
        }
      }
    },
    "leadEvidenceResolutionField": {
      "type": "object",
      "additionalProperties": false,
      "required": ["fieldId"],
      "properties": {
        "fieldId": { "type": "string", "maxLength": 512 },
        "label": { "type": "string", "maxLength": 512 },
        "requiredness": { "type": "string", "maxLength": 64 },
        "status": { "type": "string", "maxLength": 64 },
        "value": { "$ref": "#/$defs/leadDynamicValue" },
        "source": { "type": "string", "maxLength": 512 },
        "confidence": { "type": "string", "maxLength": 64 },
        "confirmation": { "type": "string", "maxLength": 64 },
        "evidenceIds": {
          "type": "array",
          "maxItems": 32,
          "items": { "type": "string", "maxLength": 512 }
        }
      }
    },
    "leadEvidenceCandidateStatus": {
      "type": "object",
      "additionalProperties": false,
      "required": ["evidenceId", "status"],
      "properties": {
        "evidenceId": { "type": "string", "maxLength": 512 },
        "status": { "type": "string", "maxLength": 64 }
      }
    },
    "leadEvidenceResolution": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "status": { "type": "string", "enum": ["ready", "not-ready"] },
        "data": { "$ref": "#/$defs/leadDynamicObject" },
        "fields": {
          "type": "array",
          "maxItems": 64,
          "items": { "$ref": "#/$defs/leadEvidenceResolutionField" }
        },
        "missingFieldIds": {
          "type": "array",
          "maxItems": 64,
          "items": { "type": "string", "maxLength": 512 }
        },
        "needsConfirmationFieldIds": {
          "type": "array",
          "maxItems": 64,
          "items": { "type": "string", "maxLength": 512 }
        },
        "conflictFieldIds": {
          "type": "array",
          "maxItems": 64,
          "items": { "type": "string", "maxLength": 512 }
        },
        "rejectedFieldIds": {
          "type": "array",
          "maxItems": 64,
          "items": { "type": "string", "maxLength": 512 }
        },
        "candidateStatuses": {
          "type": "array",
          "maxItems": 64,
          "items": { "$ref": "#/$defs/leadEvidenceCandidateStatus" }
        }
      }
    },
    "leadEvidenceProvenanceEntry": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "evidence_id",
        "field_id",
        "status",
        "source",
        "confidence",
        "confirmation",
        "observed_at",
        "updated_at"
      ],
      "properties": {
        "evidence_id": { "type": "string", "maxLength": 512 },
        "field_id": { "type": "string", "maxLength": 512 },
        "status": { "type": "string", "maxLength": 64 },
        "source": { "type": "string", "maxLength": 512 },
        "confidence": { "type": "string", "maxLength": 64 },
        "confirmation": { "type": "string", "maxLength": 64 },
        "observed_at": { "type": "number" },
        "updated_at": { "type": "number" },
        "value": { "$ref": "#/$defs/leadDynamicValue" },
        "provenance": { "$ref": "#/$defs/leadProvenance" }
      }
    },
    "conversation": {
      "type": "object",
      "required": [
        "id",
        "agent_id",
        "channel",
        "crm_status",
        "remote_name",
        "message_count",
        "last_message_at",
        "created_at"
      ],
      "additionalProperties": true,
      "properties": {
        "id": { "type": "string" },
        "agent_id": { "type": "string" },
        "channel": { "$ref": "#/$defs/channelNullable" },
        "crm_status": {
          "type": "string",
          "enum": ["open", "resolved", "spam"]
        },
        "remote_name": { "type": ["string", "null"] },
        "message_count": { "type": "number" },
        "last_message_at": {
          "type": ["string", "null"],
          "format": "date-time"
        },
        "created_at": { "type": "string", "format": "date-time" }
      }
    },
    "message": {
      "type": "object",
      "required": [
        "id",
        "agent_id",
        "conversation_id",
        "channel",
        "direction",
        "role",
        "actor",
        "message_type",
        "content_preview",
        "delivery_status",
        "provider_message_id",
        "created_at",
        "updated_at"
      ],
      "additionalProperties": true,
      "properties": {
        "id": { "type": "string" },
        "agent_id": { "type": "string" },
        "conversation_id": { "type": "string" },
        "channel": { "$ref": "#/$defs/channelNullable" },
        "direction": {
          "type": "string",
          "enum": ["inbound", "outbound"]
        },
        "role": { "type": "string" },
        "actor": { "type": ["string", "null"] },
        "message_type": { "type": "string" },
        "content_preview": { "type": "string" },
        "delivery_status": { "type": ["string", "null"] },
        "provider_message_id": { "type": ["string", "null"] },
        "error_code": { "type": ["string", "null"] },
        "error_message": { "type": ["string", "null"] },
        "provider_error_code": { "type": ["string", "null"] },
        "provider_error_subcode": { "type": ["string", "null"] },
        "template_name": { "type": ["string", "null"] },
        "template_language": { "type": ["string", "null"] },
        "created_at": { "type": "string", "format": "date-time" },
        "updated_at": { "type": "string", "format": "date-time" }
      }
    },
    "channelNullable": {
      "type": ["string", "null"],
      "enum": [
        "whatsapp",
        "instagram",
        "facebook",
        "web_test",
        "web_widget",
        null
      ]
    }
  }
}
