{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:orbiplex:schema:ledger-transfer:v1",
  "title": "LedgerTransfer v1",
  "description": "Machine-readable schema for one append-only internal transfer recorded by the host-ledger settlement rail.",
  "type": "object",
  "additionalProperties": true,
  "x-dia-workflow": "project",
  "x-dia-status": "draft",
  "x-dia-basis": [
    "doc/project/40-proposals/016-supervised-prepaid-gateway-and-escrow-mvp.md",
    "doc/project/50-requirements/requirements-007.md"
  ],
  "required": [
    "schema/v",
    "transfer/id",
    "kind",
    "from/account-id",
    "to/account-id",
    "amount",
    "unit",
    "created-at"
  ],
  "properties": {
    "schema/v": {
      "const": 1,
      "description": "Schema version."
    },
    "transfer/id": {
      "type": "string",
      "minLength": 1,
      "description": "Stable identifier of the append-only transfer fact."
    },
    "kind": {
      "type": "string",
      "enum": [
        "top-up-credit",
        "escrow-hold",
        "release",
        "partial-release",
        "refund",
        "payout-debit",
        "adjustment"
      ],
      "description": "Transfer class on the supervised ledger."
    },
    "from/account-id": {
      "type": "string",
      "minLength": 1,
      "description": "Debited ledger account identifier."
    },
    "to/account-id": {
      "type": "string",
      "minLength": 1,
      "description": "Credited ledger account identifier."
    },
    "amount": {
      "type": "integer",
      "minimum": 1,
      "description": "Transferred amount in internal minor units. For `ORC`, the value uses ORC minor units with fixed scale `2`."
    },
    "unit": {
      "const": "ORC",
      "description": "Internal settlement unit carried by the transfer in MVP. `ORC` uses fixed decimal scale `2`."
    },
    "created-at": {
      "type": "string",
      "format": "date-time",
      "description": "Timestamp when the transfer fact was recorded."
    },
    "hold/id": {
      "type": "string",
      "minLength": 1,
      "description": "Escrow hold to which the transfer belongs, when applicable."
    },
    "contract/id": {
      "type": "string",
      "minLength": 1,
      "description": "Procurement contract driving the transfer, when applicable."
    },
    "gateway-receipt/id": {
      "type": "string",
      "minLength": 1,
      "description": "Gateway receipt that justified the transfer, when applicable."
    },
    "notes": {
      "type": "string",
      "description": "Optional human-readable notes."
    },
    "policy_annotations": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "kind": {
            "enum": ["escrow-hold", "release", "partial-release", "refund"]
          }
        },
        "required": ["kind"]
      },
      "then": {
        "required": ["hold/id", "contract/id"]
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "enum": ["top-up-credit", "payout-debit"]
          }
        },
        "required": ["kind"]
      },
      "then": {
        "required": ["gateway-receipt/id"]
      }
    }
  ]
}
