{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://openfreytprotocol.example/schemas/v0/transport-tender",
  "title": "TransportTender",
  "description": "A versioned request for a selected partner to execute a bounded transport movement. Freezes the decision-relevant terms shown to the recipient at issuance; a counterproposal creates a new immutable revision rather than silently modifying terms already presented for decision.",
  "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 (TMS, ERP, etc.)."
    },
    "issuingOrganizationId": {
      "type": "string",
      "minLength": 1,
      "description": "Organization that issued this tender."
    },
    "executingOrganizationId": {
      "type": "string",
      "minLength": 1,
      "description": "Organization the tender asks to execute the movement."
    },
    "movementScope": {
      "type": "string",
      "enum": ["fullTransport", "routeLeg", "collection", "delivery"],
      "description": "Bounded scope of the movement being tendered."
    },
    "expiresAt": {
      "type": "string",
      "format": "date-time",
      "description": "ISO 8601 date-time after which the tender can no longer be accepted."
    },
    "decisionPolicy": {
      "type": "string",
      "enum": ["unanimous", "majority", "any"],
      "description": "Governs how required decisions must be aggregated before a TransportCommitment can be formed against this tender."
    },
    "cargoRequirements": {
      "type": "object",
      "description": "Cargo facts disclosed at the current disclosure phase (weight, volume, goods profile, hazard class, etc.).",
      "properties": {
        "weightKg": { "type": "number", "minimum": 0 },
        "volumeM3": { "type": "number", "minimum": 0 },
        "loadingMetres": { "type": "number", "minimum": 0 },
        "goodsProfile": { "type": "object" }
      }
    },
    "equipmentRequirements": {
      "type": "object",
      "description": "Vehicle/equipment constraints required to execute the movement.",
      "properties": {
        "vehicleBodyTypes": {
          "type": "array",
          "items": { "type": "string" }
        },
        "specialEquipment": {
          "type": "array",
          "items": { "type": "string" }
        }
      }
    },
    "stopRequirements": {
      "type": "array",
      "description": "Ordered stop facts disclosed at the current disclosure phase.",
      "items": { "type": "object" }
    },
    "timeWindowRequirements": {
      "type": "object",
      "description": "Pickup and delivery window constraints disclosed at the current disclosure phase.",
      "properties": {
        "pickupWindowStart": { "type": "string", "format": "date-time" },
        "pickupWindowEnd": { "type": "string", "format": "date-time" },
        "deliveryWindowStart": { "type": "string", "format": "date-time" },
        "deliveryWindowEnd": { "type": "string", "format": "date-time" }
      }
    },
    "commercialTerms": {
      "type": "object",
      "description": "Inline commercial terms (rate, currency, accessorials, etc.). At least one of commercialTerms or externalTermsReference is required."
    },
    "externalTermsReference": {
      "type": "string",
      "minLength": 1,
      "description": "Stable reference to commercial terms held in an external system. At least one of commercialTerms or externalTermsReference is required."
    },
    "amendmentPolicy": {
      "type": "string",
      "description": "Describes how this tender may be amended, e.g. 'counterproposal-only' or 'none'."
    },
    "cancellationPolicy": {
      "type": "string",
      "description": "Describes how this tender may be cancelled prior to acceptance."
    },
    "status": {
      "type": "string",
      "enum": ["draft", "issued", "countered", "accepted", "expired", "withdrawn"],
      "description": "Lifecycle state of the transport tender."
    },
    "revisionOf": {
      "type": ["string", "null"],
      "format": "uuid",
      "description": "Identifier of the prior TransportTender this revision supersedes, or null for the first revision in a chain."
    },
    "isImmutableRevision": {
      "type": "boolean",
      "description": "True once this tender has left 'draft' or 'issued' and its decision-relevant fields can never be mutated again; only referenced by a new revision."
    },
    "sourceExternalReferences": {
      "type": "array",
      "items": { "$ref": "#/$defs/ExternalReference" },
      "description": "External references (e.g. originating MatchProposal or prior tender revision) carried forward from the source of this tender."
    }
  },
  "required": [
    "id",
    "resourceVersion",
    "ownerOrganizationId",
    "createdAt",
    "updatedAt",
    "issuingOrganizationId",
    "executingOrganizationId",
    "movementScope",
    "expiresAt",
    "decisionPolicy",
    "amendmentPolicy",
    "cancellationPolicy",
    "status",
    "isImmutableRevision"
  ],
  "anyOf": [
    { "required": ["commercialTerms"] },
    { "required": ["externalTermsReference"] }
  ],
  "$defs": {
    "ExternalReference": {
      "type": "object",
      "properties": {
        "system": { "type": "string" },
        "type": { "type": "string" },
        "value": { "type": "string" }
      },
      "required": ["system", "type", "value"]
    }
  }
}
