{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://openfreytprotocol.example/schemas/v0/cloud-event-envelope",
  "title": "CloudEventEnvelope",
  "description": "CloudEvents 1.0 envelope emitted by an OpenFreyt broker for every domain event in the transactional outbox. The `sequence` extension attribute is always set by the publisher and is the strictly increasing outbox sequence consumers use for ordering and convergence.",
  "type": "object",
  "properties": {
    "specversion": {
      "const": "1.0",
      "description": "CloudEvents specification version."
    },
    "type": {
      "$ref": "#/$defs/EventType"
    },
    "source": {
      "type": "string",
      "format": "uri-reference",
      "minLength": 1,
      "description": "URI-reference identifying the emitting broker, e.g. urn:openfreyt:broker."
    },
    "id": {
      "type": "string",
      "minLength": 1,
      "description": "Unique event identifier (outbox row id)."
    },
    "sequence": {
      "type": "integer",
      "minimum": 1,
      "description": "Strictly increasing outbox sequence number (CloudEvents extension attribute)."
    },
    "time": {
      "type": "string",
      "format": "date-time",
      "description": "RFC 3339 timestamp of event occurrence."
    },
    "datacontenttype": {
      "type": "string",
      "description": "Media type of `data`, normally application/json."
    },
    "subject": {
      "type": "string",
      "description": "Optional subject of the event in the context of the source."
    },
    "dataschema": {
      "type": "string",
      "format": "uri",
      "description": "URI of the schema that `data` adheres to."
    },
    "data": {
      "type": "object",
      "description": "Full JSON representation of the resource after the state transition. Constrained per event type in cloud-event-types.schema.json."
    }
  },
  "required": [
    "specversion",
    "type",
    "source",
    "id",
    "sequence",
    "data"
  ],
  "$defs": {
    "EventType": {
      "type": "string",
      "description": "Closed set of domain event types emitted by the reference broker.",
      "enum": [
        "PartnerRelationshipSubmitted",
        "PartnerRelationshipAccepted",
        "PartnerRelationshipSuspended",
        "CapacityOfferCreated",
        "CapacityOfferPublished",
        "CapacityOfferWithdrawn",
        "LoadRequestCreated",
        "LoadRequestPublished",
        "LoadRequestWithdrawn",
        "MatchProposalCreated",
        "MatchDecisionRecorded",
        "ExecutionReferenceCreated",
        "ExecutionOutcomeRecorded",
        "ExecutionOutcomeCorrected",
        "ExecutionOutcomeCancelled",
        "ExecutionOutcomeException",
        "TransportTenderIssued",
        "TransportTenderCountered",
        "TransportCommitmentFormed",
        "ExecutionHandoverOffered",
        "ExecutionHandoverAcknowledged"
      ]
    }
  }
}
