{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://reachabilitylabs.org/schemas/commitment-and-reversal-record/v0.2",
  "title": "Commitment and Reversal Record v0.2",
  "description": "Canonical pre-approval record for one governed entry in a Commitment and Reversal Register.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "instrument_type",
    "record_status",
    "commitment_id",
    "revision",
    "case_binding",
    "change_request_ref",
    "protected_target",
    "before_configuration",
    "proposed_after_configuration",
    "commitment_reason",
    "path_effects",
    "evidence_invalidated",
    "reversal_contract",
    "preservation_actions",
    "approval",
    "seal"
  ],
  "properties": {
    "schema_version": { "const": "0.2" },
    "instrument_type": { "const": "commitment_and_reversal_record" },
    "record_status": { "enum": ["draft", "sealed"] },
    "commitment_id": { "type": "string", "minLength": 1, "maxLength": 128 },
    "revision": { "type": "string", "minLength": 1, "maxLength": 32 },
    "case_binding": { "$ref": "#/$defs/case_binding" },
    "change_request_ref": { "type": "string", "minLength": 1, "maxLength": 512 },
    "protected_target": { "type": "string", "minLength": 1, "maxLength": 512 },
    "decision_date": { "type": ["string", "null"], "format": "date-time" },
    "point_of_no_return": { "type": ["string", "null"], "maxLength": 2000 },
    "before_configuration": { "type": "string", "minLength": 1, "maxLength": 4000 },
    "proposed_after_configuration": { "type": "string", "minLength": 1, "maxLength": 4000 },
    "commitment_reason": { "type": "string", "minLength": 1, "maxLength": 4000 },
    "path_effects": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/path_effect" }
    },
    "evidence_invalidated": {
      "type": "array",
      "items": { "type": "string", "minLength": 1, "maxLength": 2000 }
    },
    "reversal_contract": { "$ref": "#/$defs/reversal_contract" },
    "preservation_actions": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/preservation_action" }
    },
    "approval": { "$ref": "#/$defs/approval" },
    "seal": { "oneOf": [{ "$ref": "#/$defs/seal" }, { "type": "null" }] }
  },
  "allOf": [
    {
      "if": { "properties": { "record_status": { "const": "draft" } } },
      "then": { "properties": { "seal": { "type": "null" } } }
    },
    {
      "if": { "properties": { "record_status": { "const": "sealed" } } },
      "then": { "properties": { "seal": { "$ref": "#/$defs/seal" } } }
    }
  ],
  "$defs": {
    "case_binding": {
      "type": "object",
      "additionalProperties": false,
      "required": ["case_id", "case_revision"],
      "properties": {
        "case_id": { "type": "string", "minLength": 1, "maxLength": 128 },
        "case_revision": { "type": "string", "minLength": 1, "maxLength": 64 },
        "contract_refs": {
          "type": "array",
          "items": { "type": "string", "minLength": 1, "maxLength": 512 }
        }
      }
    },
    "path_effect_value": {
      "enum": ["opens", "preserves", "narrows", "conditions", "forecloses", "reopens", "unknown"]
    },
    "path_effect": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path_ref", "effect", "mechanism", "last_reversible_point", "accepted_by"],
      "properties": {
        "path_ref": { "type": "string", "minLength": 1, "maxLength": 256 },
        "effect": { "$ref": "#/$defs/path_effect_value" },
        "mechanism": { "type": "string", "minLength": 1, "maxLength": 4000 },
        "last_reversible_point": { "type": ["string", "null"], "maxLength": 2000 },
        "accepted_by": { "type": ["string", "null"], "maxLength": 256 }
      }
    },
    "reversal_contract": {
      "type": "object",
      "additionalProperties": false,
      "required": ["reversible", "prerequisites", "cost_or_burden", "deadline_or_window", "authority_requirement", "verification_required"],
      "properties": {
        "reversible": { "enum": ["yes", "conditional", "no", "unknown"] },
        "prerequisites": {
          "type": "array",
          "items": { "type": "string", "minLength": 1, "maxLength": 2000 }
        },
        "cost_or_burden": { "type": "string", "minLength": 1, "maxLength": 4000 },
        "deadline_or_window": { "type": ["string", "null"], "maxLength": 2000 },
        "authority_requirement": { "type": "string", "minLength": 1, "maxLength": 2000 },
        "verification_required": { "type": "string", "minLength": 1, "maxLength": 2000 }
      }
    },
    "preservation_action": {
      "type": "object",
      "additionalProperties": false,
      "required": ["action", "owner", "due", "evidence_ref"],
      "properties": {
        "action": { "type": "string", "minLength": 1, "maxLength": 2000 },
        "owner": { "type": "string", "minLength": 1, "maxLength": 256 },
        "due": { "type": ["string", "null"], "format": "date-time" },
        "evidence_ref": { "type": ["string", "null"], "maxLength": 512 }
      }
    },
    "approval": {
      "type": "object",
      "additionalProperties": false,
      "required": ["authority", "conditions", "status", "approved_at"],
      "properties": {
        "authority": { "type": "string", "minLength": 1, "maxLength": 256 },
        "conditions": {
          "type": "array",
          "items": { "type": "string", "minLength": 1, "maxLength": 2000 }
        },
        "status": { "enum": ["draft", "approved", "rejected", "held", "superseded"] },
        "approved_at": { "type": ["string", "null"], "format": "date-time" }
      }
    },
    "seal": {
      "type": "object",
      "additionalProperties": false,
      "required": ["algorithm", "hash", "sealed_at", "sealed_by"],
      "properties": {
        "algorithm": { "const": "sha256-canonical-json-v1" },
        "hash": { "type": "string", "pattern": "^[0-9a-f]{64}$" },
        "sealed_at": { "type": "string", "format": "date-time" },
        "sealed_by": { "type": "string", "minLength": 1, "maxLength": 256 }
      }
    }
  }
}
