{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://openfreytprotocol.example/schemas/v0/execution-outcome",
  "title": "ExecutionOutcome",
  "description": "The final reported outcome of a transport execution, recorded after the trip has concluded, been cancelled, or entered dispute.",
  "type": "object",
  "properties": {
    "id": {
      "type": "string",
      "format": "uuid",
      "description": "UUIDv7 resource identifier."
    },
    "resourceVersion": {
      "type": "integer",
      "minimum": 1,
      "description": "Optimistic-concurrency counter. Incremented on every mutation."
    },
    "ownerOrganizationId": {
      "type": "string",
      "description": "Opaque broker-scoped identifier of the organization that owns this resource."
    },
    "createdAt": {
      "type": "string",
      "format": "date-time",
      "description": "ISO 8601 / RFC 3339 timestamp when the resource was first created."
    },
    "updatedAt": {
      "type": "string",
      "format": "date-time",
      "description": "ISO 8601 / RFC 3339 timestamp of the most recent mutation."
    },
    "externalReferences": {
      "type": "array",
      "items": { "$ref": "#/$defs/ExternalReference" },
      "description": "Namespaced references to records in external systems."
    },
    "executionReferenceId": {
      "type": "string",
      "format": "uuid",
      "description": "Identifier of the ExecutionReference this outcome closes."
    },
    "status": {
      "type": "string",
      "enum": ["completed", "cancelled", "disputed"],
      "description": "Final lifecycle status of the execution."
    },
    "outcomes": {
      "type": "object",
      "description": "Structured outcome data reported by the parties. Content is broker-defined and may include actual weights, timestamps, POD references, or dispute evidence.",
      "properties": {
        "actualPickupAt": {
          "type": "string",
          "format": "date-time",
          "description": "ISO 8601 timestamp of actual goods collection."
        },
        "actualDeliveryAt": {
          "type": "string",
          "format": "date-time",
          "description": "ISO 8601 timestamp of actual goods delivery."
        },
        "actualWeightKg": {
          "type": "number",
          "minimum": 0,
          "description": "Actual transported weight in kilograms."
        },
        "proofOfDeliveryRef": {
          "type": "string",
          "description": "Reference to a proof-of-delivery document in an external system."
        },
        "cancellationReason": {
          "type": "string",
          "description": "Free-text reason for cancellation, populated when status is 'cancelled'."
        },
        "disputeDescription": {
          "type": "string",
          "description": "Free-text description of the dispute, populated when status is 'disputed'."
        }
      }
    }
  },
  "required": [
    "id",
    "resourceVersion",
    "ownerOrganizationId",
    "createdAt",
    "updatedAt",
    "executionReferenceId",
    "status",
    "outcomes"
  ],
  "$defs": {
    "ExternalReference": {
      "type": "object",
      "properties": {
        "system": { "type": "string" },
        "type": { "type": "string" },
        "value": { "type": "string" }
      },
      "required": ["system", "type", "value"]
    }
  }
}
