{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://mandateshield.com/schemas/reconciliation-record-v1.json",
  "title": "MandateShield ReconciliationRecord v1",
  "description": "Canonical durable lease and retry record for independent provider-outcome reconciliation. A retry record never authorizes provider submission or terminal settlement by itself.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "id",
    "submission_id",
    "state",
    "attempt_count",
    "lease_owner",
    "lease_expires_at",
    "next_attempt_at",
    "last_error_code",
    "created_at",
    "updated_at",
    "resolved_at"
  ],
  "properties": {
    "id": {
      "type": "string",
      "pattern": "^recon_[a-f0-9]{32}$"
    },
    "submission_id": {
      "type": "string",
      "pattern": "^psub_[a-f0-9]{32}$"
    },
    "state": {
      "type": "string",
      "enum": [
        "PENDING",
        "LEASED",
        "RESOLVED",
        "CONFLICT",
        "EXHAUSTED"
      ]
    },
    "attempt_count": {
      "type": "integer",
      "minimum": 0
    },
    "lease_owner": {
      "type": [
        "string",
        "null"
      ],
      "minLength": 1,
      "maxLength": 240
    },
    "lease_expires_at": {
      "type": [
        "integer",
        "null"
      ],
      "minimum": 1,
      "description": "Unix milliseconds."
    },
    "next_attempt_at": {
      "type": "integer",
      "minimum": 1,
      "description": "Unix milliseconds."
    },
    "last_error_code": {
      "type": [
        "string",
        "null"
      ],
      "minLength": 1,
      "maxLength": 240
    },
    "created_at": {
      "type": "integer",
      "minimum": 1,
      "description": "Unix milliseconds."
    },
    "updated_at": {
      "type": "integer",
      "minimum": 1,
      "description": "Unix milliseconds."
    },
    "resolved_at": {
      "type": [
        "integer",
        "null"
      ],
      "minimum": 1,
      "description": "Unix milliseconds."
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "state": {
            "const": "LEASED"
          }
        },
        "required": [
          "state"
        ]
      },
      "then": {
        "properties": {
          "lease_owner": {
            "type": "string",
            "minLength": 1,
            "maxLength": 240
          },
          "lease_expires_at": {
            "type": "integer",
            "minimum": 1
          },
          "resolved_at": {
            "type": "null"
          }
        }
      },
      "else": {
        "properties": {
          "lease_owner": {
            "type": "null"
          },
          "lease_expires_at": {
            "type": "null"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "state": {
            "enum": [
              "RESOLVED",
              "CONFLICT",
              "EXHAUSTED"
            ]
          }
        },
        "required": [
          "state"
        ]
      },
      "then": {
        "properties": {
          "resolved_at": {
            "type": "integer",
            "minimum": 1
          }
        }
      },
      "else": {
        "properties": {
          "resolved_at": {
            "type": "null"
          }
        }
      }
    }
  ]
}
