{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://zaroz.cloud/contract/units/v1/event.schema.json",
  "title": "Unit event envelope",
  "description": "One event of the units event log, as returned by the events route and delivered to webhooks. Consumers must ignore unknown fields and unknown event types.",
  "type": "object",
  "required": ["seq", "id", "type", "contract_version", "occurred_at", "service_instance_id", "unit", "data"],
  "properties": {
    "seq": { "type": "integer", "minimum": 0, "description": "Gapless and strictly increasing per Workload, from 1. Only the delivery-only webhook.ping may carry 0 (the cursor of a webhook that has delivered nothing yet)." },
    "id": { "type": "string", "pattern": "^evt_[0-9a-fA-F-]{32,36}$" },
    "type": { "type": "string", "minLength": 1 },
    "contract_version": { "const": 1 },
    "occurred_at": { "type": "string", "format": "date-time" },
    "service_instance_id": { "type": "string", "minLength": 1 },
    "unit": {
      "oneOf": [
        { "type": "null" },
        {
          "type": "object",
          "required": ["id", "key", "labels"],
          "properties": {
            "id": { "type": "string", "minLength": 1 },
            "key": { "type": "string", "minLength": 1, "maxLength": 40 },
            "labels": { "type": "object", "additionalProperties": { "type": "string" } }
          }
        }
      ]
    },
    "data": { "type": "object" }
  },
  "allOf": [
    {
      "if": { "properties": { "type": { "const": "webhook.ping" } }, "required": ["type"] },
      "else": { "properties": { "seq": { "minimum": 1 } } }
    },
    {
      "if": { "properties": { "type": { "const": "unit.exited" } }, "required": ["type"] },
      "then": { "properties": { "data": { "type": "object", "required": ["code", "restarted"], "properties": { "code": { "type": "integer" }, "restarted": { "type": "boolean" } } } } }
    },
    {
      "if": { "properties": { "type": { "enum": ["unit.failed", "unit.destroyed"] } }, "required": ["type"] },
      "then": { "properties": { "data": { "type": "object", "required": ["reason"], "properties": { "reason": { "type": "string" } } } } }
    },
    {
      "if": { "properties": { "type": { "const": "units.scaled" } }, "required": ["type"] },
      "then": {
        "properties": {
          "unit": { "type": "null" },
          "data": { "type": "object", "required": ["from", "to", "reason"], "properties": { "from": { "type": "integer", "minimum": 0 }, "to": { "type": "integer", "minimum": 0 }, "reason": { "type": "string" } } }
        }
      }
    },
    {
      "if": { "properties": { "type": { "enum": ["unit.ready", "unit.allocated"] } }, "required": ["type"] },
      "then": {
        "properties": {
          "unit": { "type": "object" },
          "data": { "type": "object", "required": ["private_name"], "properties": { "private_name": { "type": "string" }, "public_endpoints": { "type": "array" } } }
        }
      }
    },
    {
      "if": { "properties": { "type": { "enum": ["unit.created", "unit.starting", "unit.accepting", "unit.not_accepting", "unit.allocation_delivered", "unit.allocation_undelivered", "unit.idle", "unit.busy", "unit.draining", "unit.stopped"] } }, "required": ["type"] },
      "then": { "properties": { "unit": { "type": "object" } } }
    }
  ]
}
