{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://openfreytprotocol.example/schemas/v0/match-proposal",
  "title": "MatchProposal",
  "description": "A broker-generated proposal pairing a CapacityOffer with a LoadRequest, pending acceptance by both parties.",
  "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."
    },
    "status": {
      "type": "string",
      "enum": ["open", "accepted", "rejected", "expired", "superseded", "withdrawn"],
      "description": "Lifecycle state of the match proposal."
    },
    "offerId": {
      "type": "string",
      "format": "uuid",
      "description": "Identifier of the CapacityOffer included in this proposal."
    },
    "requestId": {
      "type": "string",
      "format": "uuid",
      "description": "Identifier of the LoadRequest included in this proposal."
    },
    "compatibilityChecks": {
      "type": "array",
      "description": "Results of automated compatibility checks run by the broker when generating this proposal.",
      "items": { "$ref": "#/$defs/CompatibilityCheck" }
    },
    "decisionPolicy": {
      "type": "string",
      "enum": ["unanimous", "majority", "any"],
      "description": "Governs how MatchDecisions are aggregated to reach a final outcome for this proposal."
    },
    "requiredDecisions": {
      "type": "array",
      "items": {
        "type": "string",
        "enum": ["INITIATOR", "ACCEPTOR"],
        "description": "Role identifier for a party whose MatchDecision is required."
      },
      "uniqueItems": true,
      "description": "Set of party roles that must submit a MatchDecision before the proposal can be resolved."
    }
  },
  "required": [
    "id",
    "resourceVersion",
    "ownerOrganizationId",
    "createdAt",
    "updatedAt",
    "status",
    "offerId",
    "requestId",
    "compatibilityChecks",
    "decisionPolicy",
    "requiredDecisions"
  ],
  "$defs": {
    "ExternalReference": {
      "type": "object",
      "properties": {
        "system": { "type": "string" },
        "type": { "type": "string" },
        "value": { "type": "string" }
      },
      "required": ["system", "type", "value"]
    },
    "CompatibilityCheck": {
      "title": "CompatibilityCheck",
      "description": "Result of a single automated compatibility check.",
      "type": "object",
      "properties": {
        "checkId": {
          "type": "string",
          "description": "Stable identifier of the check rule, e.g. 'vehicle-body-type-match'."
        },
        "result": {
          "type": "string",
          "enum": ["passed", "failed", "abstained"],
          "description": "Outcome of the check."
        },
        "reason": {
          "type": "string",
          "description": "Optional human-readable explanation of why the check produced this result."
        }
      },
      "required": ["checkId", "result"]
    }
  }
}
