{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:orbiplex:schema:procurement-contract:v1",
  "title": "ProcurementContract v1",
  "description": "Machine-readable schema for a selected responder contract linked to a procurement question lifecycle.",
  "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",
    "doc/project/40-proposals/017-organization-subjects-and-org-did-key.md",
    "doc/project/50-requirements/requirements-008.md"
  ],
  "required": [
    "schema/v",
    "contract/id",
    "question/id",
    "room/id",
    "selected-offer/id",
    "created-at",
    "asker/node-id",
    "asker/participant-id",
    "responder/node-id",
    "responder/participant-id",
    "payment/amount",
    "payment/currency",
    "deadline-at",
    "acceptance/answer-format",
    "acceptance/min-length",
    "acceptance/max-length",
    "confirmation/mode",
    "status"
  ],
  "properties": {
    "schema/v": { "const": 1, "description": "Schema version." },
    "contract/id": {
      "type": "string",
      "minLength": 1,
      "description": "Stable identifier of the procurement contract."
    },
    "question/id": {
      "type": "string",
      "minLength": 1,
      "description": "Question lifecycle identifier to which the contract belongs."
    },
    "room/id": {
      "type": "string",
      "minLength": 1,
      "description": "Room or execution channel bound to the selected responder path."
    },
    "selected-offer/id": {
      "type": "string",
      "minLength": 1,
      "description": "Identifier of the offer chosen for contract formation."
    },
    "created-at": {
      "type": "string",
      "format": "date-time",
      "description": "Contract creation timestamp."
    },
    "asker/node-id": {
      "type": "string",
      "minLength": 1,
      "description": "Node acting for the asking side of the contract as the routing or hosting identity."
    },
    "asker/participant-id": {
      "type": "string",
      "minLength": 1,
      "description": "Participation-role identity on whose behalf the asking side entered the contract."
    },
    "asker/pod-user-id": {
      "type": "string",
      "minLength": 1,
      "description": "Hosted-user identity when the contract was created on behalf of a later pod-backed client flow. This is additive to, not a replacement for, `asker/participant-id`."
    },
    "responder/node-id": {
      "type": "string",
      "minLength": 1,
      "description": "Node selected to fulfill the answer contract as the routing or hosting identity."
    },
    "responder/participant-id": {
      "type": "string",
      "minLength": 1,
      "description": "Participation-role identity selected to fulfill or lead the responder side of the contract."
    },
    "payment/amount": {
      "type": "integer",
      "minimum": 0,
      "description": "Agreed payment amount in minor units. When `payment/currency = ORC`, the value uses ORC minor units with fixed scale `2`."
    },
    "payment/currency": {
      "type": "string",
      "minLength": 2,
      "maxLength": 16,
      "description": "Currency or settlement unit symbol for the contract payment."
    },
    "payer/account-ref": {
      "type": "string",
      "pattern": "^(participant|org|pod-user):did:key:z[1-9A-HJ-NP-Za-km-z]+$",
      "description": "Optional payer-side canonical settlement owner reference. The role lives in the identifier prefix, so no separate `payer/kind` field is used. This becomes required for the `host-ledger` rail."
    },
    "payee/account-ref": {
      "type": "string",
      "pattern": "^(participant|org|pod-user):did:key:z[1-9A-HJ-NP-Za-km-z]+$",
      "description": "Optional payee-side canonical settlement owner reference. The role lives in the identifier prefix, so no separate `payee/kind` field is used. This becomes required for the `host-ledger` rail."
    },
    "settlement/rail": {
      "type": "string",
      "enum": [
        "external-invoice",
        "host-ledger",
        "manual-transfer",
        "none"
      ],
      "description": "Settlement rail chosen outside the protocol core."
    },
    "deadline-at": {
      "type": "string",
      "format": "date-time",
      "description": "Deadline by which the responder must deliver or the contract expires."
    },
    "escrow/node-id": {
      "type": "string",
      "minLength": 1,
      "description": "Supervisory node responsible for the host-ledger escrow path."
    },
    "escrow/hold-ref": {
      "type": "string",
      "minLength": 1,
      "description": "Reference to the host-ledger hold created for this contract."
    },
    "escrow-policy/ref": {
      "type": "string",
      "pattern": "^escrow-policy:[a-z0-9][a-z0-9:-]*$",
      "description": "Escrow policy in force for this host-ledger contract."
    },
    "deadlines/work-by": {
      "type": "string",
      "format": "date-time",
      "description": "Responder delivery deadline in the host-ledger timeout cascade. This SHOULD align with `deadline-at`."
    },
    "deadlines/accept-by": {
      "type": "string",
      "format": "date-time",
      "description": "Deadline by which the payer should acknowledge delivered work."
    },
    "deadlines/dispute-by": {
      "type": "string",
      "format": "date-time",
      "description": "Last moment for opening a formal dispute under the contract policy."
    },
    "deadlines/auto-release": {
      "type": "string",
      "format": "date-time",
      "description": "Moment when escrow may auto-release if contract conditions are satisfied and no dispute is open."
    },
    "acceptance/answer-format": {
      "type": "string",
      "enum": ["plain-text", "markdown", "json", "edn", "mixed"],
      "description": "Expected answer format used for acceptance checks."
    },
    "acceptance/min-length": {
      "type": "integer",
      "minimum": 1,
      "description": "Minimum accepted answer length."
    },
    "acceptance/max-length": {
      "type": "integer",
      "minimum": 1,
      "description": "Maximum accepted answer length."
    },
    "acceptance/arbiter-set": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1
      },
      "minItems": 1,
      "description": "Arbiter identities required when arbiter confirmation is part of the contract."
    },
    "confirmation/mode": {
      "type": "string",
      "enum": ["arbiter-confirmed", "self-confirmed", "manual-review-only"],
      "description": "Confirmation mode required before settlement."
    },
    "status": {
      "type": "string",
      "enum": ["pending", "settled", "rejected", "expired", "canceled"],
      "description": "Current lifecycle status of the contract."
    },
    "transport/encryption-mode": {
      "type": "string",
      "enum": ["none", "recipient-key", "federation-policy"],
      "description": "Expected transport-level privacy mode for the narrowed execution path."
    },
    "policy_annotations": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "confirmation/mode": {
            "const": "arbiter-confirmed"
          }
        },
        "required": [
          "confirmation/mode"
        ]
      },
      "then": {
        "required": [
          "acceptance/arbiter-set"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "settlement/rail": {
            "const": "host-ledger"
          }
        },
        "required": [
          "settlement/rail"
        ]
      },
      "then": {
        "required": [
          "payer/account-ref",
          "payee/account-ref",
          "escrow/node-id",
          "escrow/hold-ref",
          "escrow-policy/ref",
          "deadlines/work-by",
          "deadlines/accept-by",
          "deadlines/dispute-by",
          "deadlines/auto-release"
        ],
        "properties": {
          "payment/currency": {
            "const": "ORC"
          }
        }
      }
    },
    {
      "if": {
        "anyOf": [
          {
            "required": [
              "deadlines/work-by"
            ]
          },
          {
            "required": [
              "deadlines/accept-by"
            ]
          },
          {
            "required": [
              "deadlines/dispute-by"
            ]
          },
          {
            "required": [
              "deadlines/auto-release"
            ]
          }
        ]
      },
      "then": {
        "required": [
          "deadlines/work-by",
          "deadlines/accept-by",
          "deadlines/dispute-by",
          "deadlines/auto-release"
        ]
      }
    }
  ]
}
