{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kaladan.dev/contracts/webhooks/v1/envelope.schema.json",
  "title": "Webhook envelope",
  "description": "The body of every webhook delivery. The envelope is stable across topics; only `payload` varies, and its shape is the topic's own — see catalog.json.",
  "type": "object",

  "properties": {
    "eventId": {
      "type": "string",
      "description": "Stable across every retry of the same event. **Deduplicate on this.** The same event will arrive twice, and it will happen on the day a network blipped between our write and your 2xx.",
      "pattern": "^evt_[0-9A-HJKMNP-TV-Z]{16,32}$"
    },
    "topic": {
      "type": "string",
      "description": "The topic, from catalog.json. `resource/action`.",
      "pattern": "^[a-z_]+/[a-z_]+$"
    },
    "apiVersion": {
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}$",
      "description": "The payload's version. Branch on it rather than guessing; a new version may add fields, and only a new version may remove one."
    },
    "occurredAt": {
      "type": "string",
      "format": "date-time",
      "description": "When the thing happened — not when it was delivered. RFC 3339, UTC. The gap between this and delivery is the retry backlog, and on a healthy system it is milliseconds."
    },
    "sequence": {
      "type": "integer",
      "minimum": 1,
      "description": "Monotonic per shop and per resource. Order by it; arrival order is not guaranteed, and `orders/updated` can reach you before `orders/created`. Gaps are normal — they are events for resources you do not subscribe to, or deliveries still being retried."
    },

    "shop": {
      "type": "object",
      "description": "Which shop this concerns. Always present, including on shop-level topics, so a multi-tenant consumer never has to infer it from the payload.",
      "properties": {
        "id": { "type": "string" },
        "handle": { "type": "string" },
        "primaryDomain": { "type": "string" }
      },
      "required": ["id", "handle"],
      "additionalProperties": false
    },

    "actor": {
      "type": "object",
      "description": "Who caused it, at the same fidelity as the Admin API's audit log. An app reacting to its own writes uses this to avoid a loop, which is the commonest reason to read it.",
      "properties": {
        "kind": {
          "enum": ["STAFF", "APP", "OPERATOR", "SYSTEM", "CUSTOMER"],
          "description": "`SYSTEM` for a scheduled job or a reconciliation worker. `CUSTOMER` for something a buyer did on a storefront."
        },
        "id": { "type": ["string", "null"] },
        "displayName": { "type": ["string", "null"] },
        "appHandle": {
          "type": ["string", "null"],
          "description": "The app that caused it, when `kind` is `APP`. Compare against your own handle to ignore your own writes."
        }
      },
      "required": ["kind"],
      "additionalProperties": false
    },

    "payload": {
      "type": "object",
      "description": "The topic's own data, **filtered against the granted scopes of the installation it is delivered to**. A field the app cannot read is absent, not null: null would say there is no value, absent says you did not ask for this and were not given it. The same topic therefore delivers different shapes to two apps.",
      "additionalProperties": true
    },

    "changed": {
      "type": "array",
      "items": { "type": "string" },
      "description": "For an update topic: which fields changed, as dotted paths. Lets a consumer ignore an event that touched nothing it cares about without diffing anything. Absent on create and delete topics, where the answer is everything and nothing."
    },

    "test": {
      "type": "boolean",
      "description": "True for a delivery from a development store or from `kaladan webhook trigger`. A consumer MUST NOT let a test event reach production side effects — a real SMS, a real charge."
    }
  },

  "required": ["eventId", "topic", "apiVersion", "occurredAt", "sequence", "shop", "actor", "payload", "test"],
  "additionalProperties": false
}
