{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:orbiplex:schema:procurement-receipt:v1",
  "title": "ProcurementReceipt v1",
  "description": "Machine-readable schema for the auditable outcome of a procurement contract.",
  "type": "object",
  "additionalProperties": true,
  "x-dia-workflow": "project",
  "x-dia-status": "draft",
  "x-dia-basis": [
    "doc/project/30-stories/story-001.md",
    "doc/project/30-stories/story-004.md",
    "doc/project/50-requirements/requirements-001.md",
    "doc/project/40-proposals/011-federated-answer-procurement-lifecycle.md",
    "doc/project/40-proposals/016-supervised-prepaid-gateway-and-escrow-mvp.md",
    "doc/project/50-requirements/requirements-007.md"
  ],
  "required": [
    "schema/v",
    "receipt/id",
    "contract/id",
    "question/id",
    "created-at",
    "payer/participant-id",
    "payee/participant-id",
    "outcome",
    "confirmation/mode"
  ],
  "properties": {
    "schema/v": { "const": 1, "description": "Schema version." },
    "receipt/id": {
      "type": "string",
      "minLength": 1,
      "description": "Stable identifier of the outcome receipt."
    },
    "contract/id": {
      "type": "string",
      "minLength": 1,
      "description": "Procurement contract to which this receipt belongs."
    },
    "question/id": {
      "type": "string",
      "minLength": 1,
      "description": "Question lifecycle identifier for audit joins."
    },
    "created-at": {
      "type": "string",
      "format": "date-time",
      "description": "Receipt creation timestamp."
    },
    "payer/participant-id": {
      "type": "string",
      "minLength": 1,
      "description": "Participation-role identity on the payer/asker side whose acceptance or refusal is being recorded."
    },
    "payee/participant-id": {
      "type": "string",
      "minLength": 1,
      "description": "Participation-role identity on the payee/responder side whose acknowledgement or outcome is being recorded."
    },
    "settled-at": {
      "type": "string",
      "format": "date-time",
      "description": "Timestamp at which settlement or equivalent terminal confirmation completed."
    },
    "outcome": {
      "type": "string",
      "enum": ["settled", "rejected", "expired", "canceled"],
      "description": "Terminal contract outcome recorded by the local node."
    },
    "confirmation/mode": {
      "type": "string",
      "enum": ["arbiter-confirmed", "self-confirmed", "manual-review-only"],
      "description": "Confirmation mode actually used for the recorded outcome."
    },
    "answer/accepted": {
      "type": "boolean",
      "description": "Whether the received answer or summary satisfied the contract criteria."
    },
    "payer/signature": {
      "type": "string",
      "minLength": 1,
      "description": "Signature or reference proving payer-side acceptance of the recorded outcome."
    },
    "payee/signature": {
      "type": "string",
      "minLength": 1,
      "description": "Signature or reference proving payee-side acknowledgement of the recorded outcome."
    },
    "arbiter/signatures": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "string",
        "minLength": 1
      },
      "description": "Arbiter confirmations when the contract required arbiter approval."
    },
    "settlement/rail": {
      "type": "string",
      "enum": [
        "external-invoice",
        "host-ledger",
        "manual-transfer",
        "none"
      ],
      "description": "Settlement rail used outside the protocol core."
    },
    "settlement/ref": {
      "type": "string",
      "minLength": 1,
      "description": "External settlement reference such as an invoice id, ledger entry id, or transfer reference."
    },
    "settlement/hold-ref": {
      "type": "string",
      "minLength": 1,
      "description": "Host-ledger hold reference from which release or refund was resolved."
    },
    "settlement/transfer-refs": {
      "type": "array",
      "minItems": 1,
      "uniqueItems": true,
      "items": {
        "type": "string",
        "minLength": 1
      },
      "description": "One or more host-ledger transfer references that completed release, partial release, refund, or payout bookkeeping for the contract."
    },
    "rejection/reason": {
      "type": "string",
      "minLength": 1,
      "description": "Human- or machine-readable reason when the answer or contract outcome was not accepted."
    },
    "policy_annotations": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "outcome": {
            "const": "settled"
          }
        },
        "required": [
          "outcome"
        ]
      },
      "then": {
        "required": [
          "settled-at",
          "answer/accepted",
          "payer/signature",
          "payee/signature"
        ],
        "properties": {
          "answer/accepted": {
            "const": true
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "confirmation/mode": {
            "const": "arbiter-confirmed"
          }
        },
        "required": [
          "confirmation/mode"
        ]
      },
      "then": {
        "required": [
          "arbiter/signatures"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "outcome": {
            "enum": ["rejected", "expired", "canceled"]
          }
        },
        "required": [
          "outcome"
        ]
      },
      "then": {
        "required": [
          "rejection/reason"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "settlement/rail": {
            "const": "host-ledger"
          }
        },
        "required": [
          "settlement/rail"
        ]
      },
      "then": {
        "required": [
          "settlement/ref",
          "settlement/hold-ref",
          "settlement/transfer-refs"
        ]
      }
    }
  ]
}
