{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:orbiplex:schema:ledger-account:v1",
  "title": "LedgerAccount v1",
  "description": "Machine-readable schema for one supervised prepaid ledger account used 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",
    "doc/project/40-proposals/007-pod-identity-and-tenancy-model.md",
    "doc/project/40-proposals/017-organization-subjects-and-org-did-key.md"
  ],
  "required": [
    "schema/v",
    "account/id",
    "account/purpose",
    "owner/kind",
    "owner/id",
    "federation/id",
    "unit",
    "status",
    "created-at"
  ],
  "properties": {
    "schema/v": {
      "const": 1,
      "description": "Schema version."
    },
    "account/id": {
      "type": "string",
      "minLength": 1,
      "description": "Stable identifier of the supervised ledger account."
    },
    "account/purpose": {
      "type": "string",
      "enum": [
        "participant-settlement",
        "pod-user-settlement",
        "org-settlement",
        "community-pool"
      ],
      "description": "Operational purpose of the account within the host-ledger rail."
    },
    "owner/kind": {
      "type": "string",
      "enum": ["participant", "pod-user", "org"],
      "description": "Identity layer that owns the account."
    },
    "owner/id": {
      "type": "string",
      "minLength": 1,
      "description": "Canonical identifier of the account owner. The allowed format depends on `owner/kind`."
    },
    "federation/id": {
      "type": "string",
      "minLength": 1,
      "description": "Authoritative federation ledger scope for this account."
    },
    "unit": {
      "const": "ORC",
      "description": "Internal settlement unit carried by this account in MVP. `ORC` uses fixed decimal scale `2`; protocol-visible integer balances therefore carry ORC minor units."
    },
    "status": {
      "type": "string",
      "enum": ["active", "suspended", "closed"],
      "description": "Administrative state of the account on the supervised ledger."
    },
    "available/balance": {
      "type": "integer",
      "minimum": 0,
      "description": "Optional exported read-model snapshot of immediately spendable balance in ORC minor units with fixed scale `2`. The append-only transfer and hold facts remain the source of truth."
    },
    "held/balance": {
      "type": "integer",
      "minimum": 0,
      "description": "Optional exported read-model snapshot of funds currently reserved by active holds in ORC minor units with fixed scale `2`."
    },
    "created-at": {
      "type": "string",
      "format": "date-time",
      "description": "Timestamp when the account was opened."
    },
    "closed-at": {
      "type": "string",
      "format": "date-time",
      "description": "Timestamp when the account was closed, if applicable."
    },
    "gateway/ref": {
      "type": "string",
      "minLength": 1,
      "description": "Optional reference to the gateway or onboarding policy under which the account was provisioned."
    },
    "disbursement/controller-kind": {
      "type": "string",
      "enum": ["owner", "council"],
      "description": "Who is allowed to authorize outbound disbursement from this account."
    },
    "disbursement/controller-id": {
      "type": "string",
      "minLength": 1,
      "description": "Canonical identifier of the disbursement controller when it differs from the owner."
    },
    "policy_annotations": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "account/purpose": {
            "const": "community-pool"
          }
        },
        "required": ["account/purpose"]
      },
      "then": {
        "properties": {
          "owner/kind": {
            "const": "org"
          },
          "disbursement/controller-kind": {
            "const": "council"
          },
          "disbursement/controller-id": {
            "pattern": "^council:did:key:z[1-9A-HJ-NP-Za-km-z]+$"
          }
        },
        "required": [
          "disbursement/controller-kind",
          "disbursement/controller-id"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "owner/kind": {
            "const": "participant"
          }
        },
        "required": ["owner/kind"]
      },
      "then": {
        "properties": {
          "owner/id": {
            "pattern": "^participant:did:key:z[1-9A-HJ-NP-Za-km-z]+$"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "owner/kind": {
            "const": "pod-user"
          }
        },
        "required": ["owner/kind"]
      },
      "then": {
        "properties": {
          "owner/id": {
            "pattern": "^pod-user:did:key:z[1-9A-HJ-NP-Za-km-z]+$"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "owner/kind": {
            "const": "org"
          }
        },
        "required": ["owner/kind"]
      },
      "then": {
        "properties": {
          "owner/id": {
            "pattern": "^org:did:key:z[1-9A-HJ-NP-Za-km-z]+$"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "status": {
            "const": "closed"
          }
        },
        "required": ["status"]
      },
      "then": {
        "required": ["closed-at"]
      }
    }
  ]
}
