{
  "openapi": "3.1.0",
  "info": {
    "title": "OHN-15 Consent Management and Ledger",
    "version": "1.0.0",
    "description": "Consent grant, revoke, renew, enforcement decision, reports. Default deny. Authentication: bearer token (OHN gateway token (ES256, audience ohn-15, claims role, jurisdiction, organization_id, purpose)). All POSTs require an Idempotency-Key header (8-128 characters)."
  },
  "servers": [
    {
      "url": "/api/ohn/v1"
    }
  ],
  "security": [
    {
      "bearer": []
    }
  ],
  "paths": {
    "/consents": {
      "post": {
        "operationId": "requestConsent",
        "x-inventory-id": "OHN-15-API-001",
        "summary": "Request a consent (W001 start)",
        "x-roles": [
          "patient",
          "consent_steward"
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ConsentGrantCommand"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "x-response-schema": "#/components/schemas/ConsentView"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "404": {
            "$ref": "#/components/responses/Error"
          },
          "409": {
            "$ref": "#/components/responses/Error"
          },
          "422": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/Error"
          },
          "503": {
            "$ref": "#/components/responses/Error"
          }
        },
        "x-previous-operation-id": "ohn-15_post_consents",
        "tags": [
          "OHN-15"
        ],
        "security": [
          {
            "bearer": []
          }
        ]
      }
    },
    "/patients/{id}/consents": {
      "get": {
        "operationId": "listPatientConsents",
        "x-inventory-id": "OHN-15-API-002",
        "summary": "List a patient's consents",
        "x-roles": [
          "patient",
          "consent_steward"
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/Id"
          },
          {
            "name": "status",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "$ref": "#/components/parameters/Cursor"
          }
        ],
        "responses": {
          "200": {
            "description": "Page of consents",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "x-response-schema": "#/components/schemas/ConsentPage"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "404": {
            "$ref": "#/components/responses/Error"
          }
        },
        "x-previous-operation-id": "ohn-15_get_patients-id-consents",
        "tags": [
          "OHN-15"
        ],
        "security": [
          {
            "bearer": []
          }
        ]
      }
    },
    "/patients/{id}/consent-access-log": {
      "get": {
        "operationId": "patientAccessLog",
        "summary": "Recipient access history (P007)",
        "parameters": [
          {
            "$ref": "#/components/parameters/Id"
          },
          {
            "$ref": "#/components/parameters/Cursor"
          }
        ],
        "responses": {
          "200": {
            "description": "Decisions recorded for the patient",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "404": {
            "$ref": "#/components/responses/Error"
          }
        },
        "x-previous-operation-id": "ohn-15_get_patients-id-consent-access-log",
        "tags": [
          "OHN-15"
        ],
        "security": [
          {
            "bearer": []
          }
        ]
      }
    },
    "/consents/worklist": {
      "get": {
        "operationId": "consentWorklist",
        "summary": "Consents awaiting action (steward)",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "403": {
            "$ref": "#/components/responses/Error"
          }
        },
        "x-previous-operation-id": "ohn-15_get_consents-worklist",
        "tags": [
          "OHN-15"
        ],
        "security": [
          {
            "bearer": []
          }
        ]
      }
    },
    "/consents/{id}": {
      "get": {
        "operationId": "consentDetail",
        "summary": "Consent detail with workflow history",
        "parameters": [
          {
            "$ref": "#/components/parameters/Id"
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "x-response-schema": "#/components/schemas/ConsentView"
                }
              }
            }
          },
          "404": {
            "$ref": "#/components/responses/Error"
          }
        },
        "x-previous-operation-id": "ohn-15_get_consents-id",
        "tags": [
          "OHN-15"
        ],
        "security": [
          {
            "bearer": []
          }
        ]
      }
    },
    "/consents/{id}/transitions": {
      "post": {
        "operationId": "transitionConsent",
        "summary": "Advance W001 (informed_choice, granted, denied)",
        "parameters": [
          {
            "$ref": "#/components/parameters/Id"
          },
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/TransitionCommand"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "409": {
            "$ref": "#/components/responses/Error"
          },
          "422": {
            "$ref": "#/components/responses/Error"
          }
        },
        "x-previous-operation-id": "ohn-15_post_consents-id-transitions",
        "tags": [
          "OHN-15"
        ],
        "security": [
          {
            "bearer": []
          }
        ]
      }
    },
    "/consents/{id}/revoke": {
      "post": {
        "operationId": "revokeConsent",
        "x-inventory-id": "OHN-15-API-003",
        "summary": "Revoke (W002)",
        "parameters": [
          {
            "$ref": "#/components/parameters/Id"
          },
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RevocationCommand"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Revoked or scheduled; downstream delivery is reported as pending until the outbox relay confirms it",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "404": {
            "$ref": "#/components/responses/Error"
          },
          "409": {
            "$ref": "#/components/responses/Error"
          },
          "422": {
            "$ref": "#/components/responses/Error"
          }
        },
        "x-previous-operation-id": "ohn-15_post_consents-id-revoke",
        "tags": [
          "OHN-15"
        ],
        "security": [
          {
            "bearer": []
          }
        ]
      }
    },
    "/consents/{id}/representative-review": {
      "post": {
        "operationId": "reviewRepresentative",
        "summary": "Representative approval (F003, steward only)",
        "parameters": [
          {
            "$ref": "#/components/parameters/Id"
          },
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RepresentativeReviewCommand"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "422": {
            "$ref": "#/components/responses/Error"
          },
          "503": {
            "$ref": "#/components/responses/Error"
          }
        },
        "x-previous-operation-id": "ohn-15_post_consents-id-representative-review",
        "tags": [
          "OHN-15"
        ],
        "security": [
          {
            "bearer": []
          }
        ]
      }
    },
    "/consents/{id}/renew": {
      "post": {
        "operationId": "renewConsent",
        "summary": "Renewal (F004)",
        "parameters": [
          {
            "$ref": "#/components/parameters/Id"
          },
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RenewalCommand"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "409": {
            "$ref": "#/components/responses/Error"
          },
          "422": {
            "$ref": "#/components/responses/Error"
          }
        },
        "x-previous-operation-id": "ohn-15_post_consents-id-renew",
        "tags": [
          "OHN-15"
        ],
        "security": [
          {
            "bearer": []
          }
        ]
      }
    },
    "/consents/decisions": {
      "post": {
        "operationId": "decideAccess",
        "summary": "Enforcement decision (service role only)",
        "parameters": [
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/DecisionCommand"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "permit or deny; deny always carries error_code",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "x-response-schema": "#/components/schemas/DecisionResult"
                }
              }
            }
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "503": {
            "$ref": "#/components/responses/Error"
          }
        },
        "x-previous-operation-id": "ohn-15_post_consents-decisions",
        "tags": [
          "OHN-15"
        ],
        "security": [
          {
            "bearer": []
          }
        ]
      }
    },
    "/consents/maintenance/sweep": {
      "post": {
        "operationId": "sweepConsents",
        "summary": "Apply expiry and scheduled revocation (W003)",
        "parameters": [
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "responses": {
          "200": {
            "description": "Counts of expired, revoked, activated",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        },
        "x-previous-operation-id": "ohn-15_post_consents-maintenance-sweep",
        "tags": [
          "OHN-15"
        ],
        "security": [
          {
            "bearer": []
          }
        ]
      }
    },
    "/consents/maintenance/drain-outbox": {
      "post": {
        "operationId": "drainConsentOutbox",
        "summary": "Relay pending events; with no bus configured reports pending only",
        "parameters": [
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "responses": {
          "200": {
            "description": "delivered / pending counts",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        },
        "x-previous-operation-id": "ohn-15_post_consents-maintenance-drain-outbox",
        "tags": [
          "OHN-15"
        ],
        "security": [
          {
            "bearer": []
          }
        ]
      }
    },
    "/consent-policy": {
      "get": {
        "operationId": "readConsentPolicy",
        "summary": "Jurisdiction policy versions",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        },
        "x-previous-operation-id": "ohn-15_get_consent-policy",
        "tags": [
          "OHN-15"
        ],
        "security": [
          {
            "bearer": []
          }
        ]
      },
      "post": {
        "operationId": "draftConsentPolicy",
        "summary": "Draft a policy version (steward)",
        "parameters": [
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/PolicyDraftCommand"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Draft created",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "422": {
            "$ref": "#/components/responses/Error"
          }
        },
        "x-previous-operation-id": "ohn-15_post_consent-policy",
        "tags": [
          "OHN-15"
        ],
        "security": [
          {
            "bearer": []
          }
        ]
      }
    },
    "/consent-policy/{version}/activate": {
      "post": {
        "operationId": "activateConsentPolicy",
        "summary": "Activate (dual control: not the drafter)",
        "parameters": [
          {
            "name": "version",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer",
              "description": "Policy version number; a value below 1 or not a number is 404"
            }
          },
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "responses": {
          "200": {
            "description": "Active",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "409": {
            "$ref": "#/components/responses/Error"
          }
        },
        "x-previous-operation-id": "ohn-15_post_consent-policy-version-activate",
        "tags": [
          "OHN-15"
        ],
        "security": [
          {
            "bearer": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ActivateCommand"
              }
            }
          }
        }
      }
    },
    "/consents/reports/{reportId}": {
      "get": {
        "operationId": "runConsentReport",
        "summary": "Run OHN-15-R001, R002 or R003",
        "parameters": [
          {
            "name": "reportId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "description": "OHN-15-R001, OHN-15-R002 or OHN-15-R003; any other value is 404"
            }
          },
          {
            "name": "period_from",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "period_to",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "purpose",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "recipient",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "patient_scope",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "facility",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "reason",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "cursor",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Report",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "x-response-schema": "#/components/schemas/Report"
                }
              }
            }
          },
          "422": {
            "$ref": "#/components/responses/Error"
          }
        },
        "x-previous-operation-id": "ohn-15_get_consents-reports-reportid",
        "tags": [
          "OHN-15"
        ],
        "security": [
          {
            "bearer": []
          }
        ]
      }
    },
    "/consents/reports/{reportId}/export": {
      "post": {
        "operationId": "createConsentExport",
        "summary": "Create a signed short-lived export (csv|pdf)",
        "parameters": [
          {
            "name": "reportId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "description": "OHN-15-R001, OHN-15-R002 or OHN-15-R003; any other value is 404"
            }
          },
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "responses": {
          "201": {
            "description": "download_path valid for five minutes, owner only",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        },
        "x-previous-operation-id": "ohn-15_post_consents-reports-reportid-export",
        "tags": [
          "OHN-15"
        ],
        "security": [
          {
            "bearer": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ExportCommand"
              }
            }
          }
        }
      }
    },
    "/consents/exports/{token}": {
      "get": {
        "operationId": "fetchConsentExport",
        "summary": "Download; 410 when expired or when consent changed after creation",
        "parameters": [
          {
            "name": "token",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "File",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "404": {
            "$ref": "#/components/responses/Error"
          },
          "410": {
            "$ref": "#/components/responses/Error"
          }
        },
        "x-previous-operation-id": "ohn-15_get_consents-exports-token",
        "tags": [
          "OHN-15"
        ],
        "security": [
          {
            "bearer": []
          }
        ]
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearer": {
        "type": "http",
        "scheme": "bearer"
      }
    },
    "parameters": {
      "Id": {
        "name": "id",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string",
          "format": "uuid"
        }
      },
      "Cursor": {
        "name": "cursor",
        "in": "query",
        "schema": {
          "type": "string",
          "maxLength": 64
        }
      },
      "IdempotencyKey": {
        "name": "Idempotency-Key",
        "in": "header",
        "required": true,
        "schema": {
          "type": "string",
          "minLength": 8,
          "maxLength": 128
        }
      }
    },
    "responses": {
      "Error": {
        "description": "Typed error",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "string"
          },
          "message": {
            "type": "string"
          },
          "details": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            },
            "description": "field -> code; never contains submitted values"
          }
        }
      },
      "Code": {
        "type": "string",
        "pattern": "^[a-z][a-z0-9_.-]{1,63}$",
        "description": "Controlled-vocabulary code from the active jurisdiction policy"
      },
      "ConsentGrantCommand": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "patient_id",
          "recipient_org",
          "data_categories",
          "purpose",
          "start_at",
          "expires_at",
          "legal_basis"
        ],
        "properties": {
          "patient_id": {
            "type": "string",
            "format": "uuid"
          },
          "recipient_org": {
            "type": "string",
            "format": "uuid"
          },
          "data_categories": {
            "description": "Array of codes (1-64, unique); a comma, semicolon or space separated string is also accepted and normalised"
          },
          "purpose": {
            "$ref": "#/components/schemas/Code"
          },
          "start_at": {
            "type": "string",
            "format": "date-time"
          },
          "expires_at": {
            "type": "string",
            "format": "date-time"
          },
          "legal_basis": {
            "$ref": "#/components/schemas/Code"
          }
        }
      },
      "TransitionCommand": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "expected_version",
          "to_state",
          "reason"
        ],
        "properties": {
          "expected_version": {
            "type": "integer",
            "minimum": 1
          },
          "to_state": {
            "enum": [
              "informed_choice",
              "granted",
              "denied"
            ]
          },
          "evidence_ref": {
            "type": "string",
            "format": "uuid"
          },
          "reason": {
            "type": "string",
            "minLength": 10,
            "maxLength": 4096
          }
        }
      },
      "RevocationCommand": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "consent_id",
          "revocation_scope",
          "reason",
          "requested_by",
          "effective_at",
          "expected_version"
        ],
        "properties": {
          "consent_id": {
            "type": "string",
            "format": "uuid"
          },
          "revocation_scope": {
            "enum": [
              "full",
              "data_categories"
            ]
          },
          "revoked_data_categories": {
            "description": "Array of codes; required exactly when revocation_scope is data_categories; a separated string is also accepted"
          },
          "reason": {
            "type": "string",
            "minLength": 10
          },
          "requested_by": {
            "type": "string",
            "format": "uuid",
            "description": "Must equal the authenticated principal"
          },
          "effective_at": {
            "type": "string",
            "format": "date-time"
          },
          "expected_version": {
            "type": "integer",
            "minimum": 1
          }
        }
      },
      "RepresentativeReviewCommand": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "consent_id",
          "representative_ref",
          "authority_evidence",
          "decision",
          "reason",
          "expected_version"
        ],
        "properties": {
          "consent_id": {
            "type": "string",
            "format": "uuid"
          },
          "representative_ref": {
            "type": "string",
            "format": "uuid"
          },
          "authority_evidence": {
            "type": "string",
            "format": "uuid"
          },
          "decision": {
            "enum": [
              "approve",
              "reject"
            ]
          },
          "reason": {
            "type": "string",
            "minLength": 10
          },
          "expected_version": {
            "type": "integer",
            "minimum": 1
          }
        }
      },
      "RenewalCommand": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "consent_id",
          "new_expiry",
          "patient_confirmation",
          "expected_version"
        ],
        "properties": {
          "consent_id": {
            "type": "string",
            "format": "uuid"
          },
          "new_expiry": {
            "type": "string",
            "format": "date-time"
          },
          "scope_changes": {
            "type": "string",
            "maxLength": 8192
          },
          "patient_confirmation": {
            "type": "string",
            "format": "uuid"
          },
          "expected_version": {
            "type": "integer",
            "minimum": 1
          }
        }
      },
      "DecisionCommand": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "patient_id",
          "recipient_org",
          "resource_class",
          "purpose",
          "facility_id"
        ],
        "properties": {
          "patient_id": {
            "type": "string",
            "format": "uuid"
          },
          "recipient_org": {
            "type": "string",
            "format": "uuid"
          },
          "resource_class": {
            "$ref": "#/components/schemas/Code"
          },
          "purpose": {
            "$ref": "#/components/schemas/Code"
          },
          "facility_id": {
            "type": "string",
            "format": "uuid"
          },
          "channel": {
            "enum": [
              "online",
              "cache_revalidation",
              "async_export"
            ],
            "default": "online"
          },
          "cached_decision_epoch": {
            "type": "integer",
            "minimum": 1
          }
        }
      },
      "DecisionResult": {
        "type": "object",
        "required": [
          "decision",
          "decision_epoch",
          "cache"
        ],
        "properties": {
          "decision": {
            "enum": [
              "permit",
              "deny"
            ]
          },
          "error_code": {
            "enum": [
              null,
              "no_consent",
              "scope_mismatch",
              "consent_revoked",
              "consent_expired",
              "consent_not_yet_effective",
              "consent_pending",
              "consent_denied"
            ]
          },
          "consent_id": {
            "type": [
              "string",
              "null"
            ]
          },
          "decision_epoch": {
            "type": "integer"
          },
          "stale_cache_rejected": {
            "type": "boolean"
          },
          "cache": {
            "type": "object",
            "properties": {
              "ttl_seconds": {
                "type": "integer",
                "minimum": 0
              },
              "valid_until": {
                "type": [
                  "string",
                  "null"
                ]
              }
            }
          }
        }
      },
      "PolicyDraftCommand": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "config",
          "reason"
        ],
        "properties": {
          "config": {
            "type": "object",
            "description": "See schema PolicyConfig; validated server-side (codes, no duplicates, strict keys)."
          },
          "reason": {
            "type": "string",
            "minLength": 10
          }
        }
      },
      "ConsentView": {
        "type": "object",
        "required": [
          "id",
          "status",
          "version"
        ],
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "status": {
            "enum": [
              "requested",
              "identity_authorized",
              "informed_choice",
              "granted",
              "effective",
              "denied",
              "revocation_requested",
              "revoked",
              "expired"
            ],
            "description": "Computed from time, so expiry and revocation apply even before a sweep runs"
          },
          "stored_status": {
            "type": "string"
          },
          "version": {
            "type": "integer"
          },
          "patient_id": {
            "type": "string"
          },
          "recipient_org": {
            "type": "string"
          },
          "data_categories": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "purpose": {
            "type": "string"
          },
          "legal_basis": {
            "type": "string"
          },
          "start_at": {
            "type": "string"
          },
          "expires_at": {
            "type": "string"
          },
          "receipt": {
            "type": "object"
          }
        }
      },
      "ConsentPage": {
        "type": "object",
        "properties": {
          "items": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/ConsentView"
            }
          },
          "next_cursor": {
            "type": [
              "string",
              "null"
            ]
          },
          "epoch": {
            "type": "integer"
          }
        }
      },
      "Report": {
        "type": "object",
        "required": [
          "report_id",
          "columns",
          "rows",
          "state"
        ],
        "properties": {
          "report_id": {
            "type": "string"
          },
          "columns": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "rows": {
            "type": "array",
            "items": {
              "type": "object"
            }
          },
          "summary": {
            "type": "object"
          },
          "next_cursor": {
            "type": [
              "string",
              "null"
            ]
          },
          "state": {
            "enum": [
              "rendered",
              "no_authorized_data"
            ]
          },
          "suppression": {
            "type": "object",
            "properties": {
              "min_cell_size": {
                "type": [
                  "integer",
                  "null"
                ]
              },
              "applied": {
                "type": "boolean"
              }
            }
          }
        }
      },
      "PolicyConfig": {
        "type": "object",
        "required": [
          "purposes",
          "legal_bases",
          "data_categories",
          "max_consent_duration_days",
          "min_aggregate_cell_size"
        ],
        "properties": {
          "purposes": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Code"
            }
          },
          "legal_bases": {
            "type": "array",
            "items": {
              "type": "object",
              "required": [
                "code",
                "requires_affirmative_consent"
              ],
              "properties": {
                "code": {
                  "$ref": "#/components/schemas/Code"
                },
                "requires_affirmative_consent": {
                  "type": "boolean"
                }
              }
            }
          },
          "data_categories": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Code"
            }
          },
          "max_consent_duration_days": {
            "type": [
              "integer",
              "null"
            ],
            "description": "null is an explicit jurisdiction decision that no maximum applies"
          },
          "min_aggregate_cell_size": {
            "type": "integer",
            "minimum": 1
          }
        }
      },
      "ExportCommand": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "format"
        ],
        "properties": {
          "format": {
            "enum": [
              "csv",
              "pdf"
            ]
          },
          "filters": {
            "type": "object",
            "description": "Same filters as the report (period_from, period_to, purpose, recipient, patient_scope, facility, reason); strict"
          }
        }
      },
      "ActivateCommand": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "reason"
        ],
        "properties": {
          "reason": {
            "type": "string",
            "minLength": 10,
            "maxLength": 4096
          }
        }
      }
    }
  }
}
