openapi: 3.1.0
info:
  title: OHN-13 Clinical Document Registry
  version: 1.0.0
  description: >-
    Document ingestion with malware scanning and digest verification (W001), short-lived one-time retrieval with
    receipts (W002), and amendment with preserved versions (W003). Every POST requires an Idempotency-Key. Document bodies
    never appear in URLs, events, logs or reports; patient references are never returned.
servers:
  - url: https://opeshealthnetwork.com
security:
  - bearerAuth: []
tags:
  - name: Health
  - name: Synthetic
  - name: Documents
  - name: Amendment
  - name: Retrieval
  - name: Reports
  - name: Patient access
paths:
  /health/ohn-13:
    get:
      operationId: ohn13_health
      summary: Liveness and database check
      tags: [Health]
      security: []
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '404': { $ref: '#/components/responses/E404' }
  /api/ohn/v1/documents/synthetic-token:
    post:
      operationId: ohn13_syntheticToken
      summary: Issue a synthetic token (synthetic mode only)
      tags: [Synthetic]
      security: []
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/SyntheticToken' }
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '404': { $ref: '#/components/responses/E404' }
  /api/ohn/v1/documents:
    get:
      operationId: ohn13_listDocuments
      summary: Document index (patient masked)
      tags: [Documents]
      parameters:
        - name: state
          in: query
          required: false
          schema: { type: string, maxLength: 64 }
        - name: document_type
          in: query
          required: false
          schema: { type: string, maxLength: 64 }
        - name: patient_ref
          in: query
          required: false
          schema: { type: string, format: uuid }
        - name: source_org
          in: query
          required: false
          schema: { type: string, format: uuid }
        - name: cursor
          in: query
          required: false
          schema: { type: string, maxLength: 64 }
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '410': { $ref: '#/components/responses/E410' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/context:
    get:
      operationId: ohn13_context
      summary: Caller principal and permitted actions
      tags: [Documents]
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '410': { $ref: '#/components/responses/E410' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/vocabulary:
    get:
      operationId: ohn13_vocabulary
      summary: Controlled vocabularies
      tags: [Documents]
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '410': { $ref: '#/components/responses/E410' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/monitor:
    get:
      operationId: ohn13_monitor
      summary: Ingest KPIs, 14-day trend and exceptions
      tags: [Documents]
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '410': { $ref: '#/components/responses/E410' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/amendments:
    get:
      operationId: ohn13_listAmendments
      summary: Amendment requests
      tags: [Amendment]
      parameters:
        - name: state
          in: query
          required: false
          schema: { type: string, maxLength: 64 }
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '410': { $ref: '#/components/responses/E410' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/amendments/{id}/decide:
    post:
      operationId: ohn13_decideAmendment
      summary: Steward verifies (links new version, preserves old) or rejects
      tags: [Amendment]
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string, format: uuid }
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/AmendmentDecision' }
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '400': { $ref: '#/components/responses/E400' }
        '409': { $ref: '#/components/responses/E409' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/contents:
    post:
      operationId: ohn13_uploadContent
      summary: Store content and scan it; returns storage_locator and sha256
      tags: [Documents]
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/Content' }
      responses:
        '201':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '400': { $ref: '#/components/responses/E400' }
        '409': { $ref: '#/components/responses/E409' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/register:
    post:
      operationId: ohn13_registerDocument
      x-inventory-id: OHN-13-API-001
      summary: 'Register a document (F001, W001): 201 indexed, 202 quarantined, 422 rejected'
      tags: [Documents]
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/Register' }
      responses:
        '201':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '202':
          description: Quarantined for content review
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '400': { $ref: '#/components/responses/E400' }
        '409': { $ref: '#/components/responses/E409' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/deliveries/{token}:
    get:
      operationId: ohn13_deliverDocument
      summary: One-time, requester-bound, 5-minute delivery of the document body
      tags: [Retrieval]
      parameters:
        - name: token
          in: path
          required: true
          schema: { type: string, pattern: "^[A-Za-z0-9_-]{20,64}$" }
      responses:
        '200':
          description: Success; the body is the stored document with its own media type; headers X-Content-SHA256 and Content-Security-Policy
          content:
            application/pdf: { schema: { type: string, format: binary } }
            text/plain: { schema: { type: string, format: binary } }
            image/png: { schema: { type: string, format: binary } }
            image/jpeg: { schema: { type: string, format: binary } }
            application/fhir+json: { schema: { type: string, format: binary } }
            application/xml: { schema: { type: string, format: binary } }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '410': { $ref: '#/components/responses/E410' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/patients/{patientRef}/patient-view:
    get:
      operationId: ohn13_patientDocumentView
      summary: The patient's own documents for the OHN-31 patient chart (role patient, purpose patient_access; own record only, otherwise 404; every read audited)
      tags: [Patient access]
      parameters:
        - name: patientRef
          in: path
          required: true
          description: The patient's own OHN-03 id (must equal the token subject)
          schema: { type: string, format: uuid }
      responses:
        '200':
          description: Success
          content:
            application/json: { schema: { type: object, additionalProperties: true } }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/patients/{patientRef}/patient-view/{id}/retrieve:
    post:
      operationId: ohn13_patientDocumentRetrieve
      summary: A one-time delivery link for one of the patient's own documents (empty body)
      tags: [Patient access]
      parameters:
        - name: patientRef
          in: path
          required: true
          schema: { type: string, format: uuid }
        - name: id
          in: path
          required: true
          schema: { type: string, format: uuid }
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: false
        content:
          application/json: { schema: { type: object, additionalProperties: false } }
      responses:
        '201':
          description: Success
          content:
            application/json: { schema: { type: object, additionalProperties: true } }
        '400': { $ref: '#/components/responses/E400' }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '409': { $ref: '#/components/responses/E409' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/patient-deliveries/{token}:
    get:
      operationId: ohn13_patientDeliverDocument
      summary: One-time, patient-bound delivery of the document body issued by the patient-view retrieve
      tags: [Patient access]
      parameters:
        - name: token
          in: path
          required: true
          schema: { type: string, pattern: "^[A-Za-z0-9_-]{20,64}$" }
      responses:
        '200':
          description: Success; the body is the stored document with its own media type; headers X-Content-SHA256 and Content-Security-Policy
          content:
            application/pdf: { schema: { type: string, format: binary } }
            text/plain: { schema: { type: string, format: binary } }
            image/png: { schema: { type: string, format: binary } }
            image/jpeg: { schema: { type: string, format: binary } }
            application/fhir+json: { schema: { type: string, format: binary } }
            application/xml: { schema: { type: string, format: binary } }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '410': { $ref: '#/components/responses/E410' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/reports/{reportId}:
    get:
      operationId: ohn13_runReport
      summary: Run OHN-13-R001..R003
      tags: [Reports]
      parameters:
        - name: reportId
          in: path
          required: true
          schema: { type: string, enum: [OHN-13-R001, OHN-13-R002, OHN-13-R003] }
        - name: from
          in: query
          required: false
          description: ISO date-time with offset; a malformed filter answers 422 invalid_filter
          schema: { type: string, maxLength: 64 }
        - name: to
          in: query
          required: false
          description: ISO date-time with offset; a malformed filter answers 422 invalid_filter
          schema: { type: string, maxLength: 64 }
        - name: facility
          in: query
          required: false
          description: Organisation id (uuid); validated by the report filter rules
          schema: { type: string, maxLength: 64 }
        - name: source
          in: query
          required: false
          description: Source organisation id (uuid); validated by the report filter rules
          schema: { type: string, maxLength: 64 }
        - name: document_type
          in: query
          required: false
          schema: { type: string, maxLength: 64 }
        - name: type
          in: query
          required: false
          schema: { type: string, maxLength: 64 }
        - name: status
          in: query
          required: false
          schema: { type: string, maxLength: 64 }
        - name: cursor
          in: query
          required: false
          schema: { type: string, maxLength: 64 }
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '410': { $ref: '#/components/responses/E410' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/reports/{reportId}/export:
    post:
      operationId: ohn13_exportReport
      summary: Signed, 5-minute, owner-only CSV or PDF export
      tags: [Reports]
      parameters:
        - name: reportId
          in: path
          required: true
          schema: { type: string, enum: [OHN-13-R001, OHN-13-R002, OHN-13-R003] }
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/Export' }
      responses:
        '201':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '400': { $ref: '#/components/responses/E400' }
        '409': { $ref: '#/components/responses/E409' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/exports/{token}:
    get:
      operationId: ohn13_downloadExport
      summary: Download an export (audited)
      tags: [Reports]
      parameters:
        - name: token
          in: path
          required: true
          schema: { type: string, pattern: "^[A-Za-z0-9_-]{20,64}$" }
      responses:
        '200':
          description: Success
          content:
            text/csv: { schema: { type: string, format: binary } }
            application/pdf: { schema: { type: string, format: binary } }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '410': { $ref: '#/components/responses/E410' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/{id}/metadata:
    get:
      operationId: ohn13_getMetadata
      x-inventory-id: OHN-13-API-002
      summary: Metadata, scan, freshness and history (no content)
      tags: [Documents]
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string, format: uuid }
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '410': { $ref: '#/components/responses/E410' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/{id}/versions:
    get:
      operationId: ohn13_listVersions
      summary: All versions, oldest preserved
      tags: [Amendment]
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string, format: uuid }
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '410': { $ref: '#/components/responses/E410' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/{id}/receipts:
    get:
      operationId: ohn13_listReceipts
      summary: Retrieval receipts (access audit)
      tags: [Retrieval]
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string, format: uuid }
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '410': { $ref: '#/components/responses/E410' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/{id}/integrity-checks:
    get:
      operationId: ohn13_listIntegrityChecks
      summary: Integrity re-check history
      tags: [Documents]
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string, format: uuid }
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '410': { $ref: '#/components/responses/E410' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/{id}/retrieve:
    post:
      operationId: ohn13_retrieveDocument
      x-inventory-id: OHN-13-API-003
      summary: 'Request retrieval (F002, W002): delivery link or metadata'
      tags: [Retrieval]
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string, format: uuid }
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/Retrieve' }
      responses:
        '201':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '400': { $ref: '#/components/responses/E400' }
        '409': { $ref: '#/components/responses/E409' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/{id}/quarantine-decision:
    post:
      operationId: ohn13_quarantineDecision
      summary: Release (suspicious only) or reject a quarantined document
      tags: [Documents]
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string, format: uuid }
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/QuarantineDecision' }
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '400': { $ref: '#/components/responses/E400' }
        '409': { $ref: '#/components/responses/E409' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/{id}/revoke:
    post:
      operationId: ohn13_revokeDocument
      summary: Revoke; outstanding delivery links stop working
      tags: [Documents]
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string, format: uuid }
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/Reasoned' }
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '400': { $ref: '#/components/responses/E400' }
        '409': { $ref: '#/components/responses/E409' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/{id}/verify-integrity:
    post:
      operationId: ohn13_verifyIntegrity
      summary: Recompute digests and rescan every version
      tags: [Documents]
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string, format: uuid }
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/Empty' }
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '400': { $ref: '#/components/responses/E400' }
        '409': { $ref: '#/components/responses/E409' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/{id}/amendments:
    post:
      operationId: ohn13_requestAmendment
      summary: Request a version correction (F003)
      tags: [Amendment]
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string, format: uuid }
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/Amendment' }
      responses:
        '201':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '400': { $ref: '#/components/responses/E400' }
        '409': { $ref: '#/components/responses/E409' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/documents/synthetic/setup:
    post:
      operationId: ohn13_syntheticSetup
      summary: Configure synthetic upstream fakes
      tags: [Synthetic]
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/SyntheticSetup' }
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '400': { $ref: '#/components/responses/E400' }
        '409': { $ref: '#/components/responses/E409' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
components:
  securitySchemes:
    bearerAuth: { type: http, scheme: bearer, bearerFormat: JWT }
  parameters:
    IdempotencyKey:
      name: Idempotency-Key
      in: header
      required: true
      schema: { type: string, minLength: 8, maxLength: 128, pattern: "^[A-Za-z0-9_.:-]+$" }
  responses:
    E400:
      description: Malformed request or missing Idempotency-Key
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Error' }
    E401:
      description: Unauthenticated
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Error' }
    E403:
      description: Denied by policy (audited)
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Error' }
    E404:
      description: Not found or not disclosed
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Error' }
    E409:
      description: Stale version, invalid transition, conflict or reused idempotency key
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Error' }
    E410:
      description: 'Gone: link expired, used or revoked'
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Error' }
    E422:
      description: Validation failure or domain refusal
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Error' }
    E429:
      description: Rate limited; see Retry-After
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Error' }
    E503:
      description: Jurisdiction service or another dependency unavailable; nothing changed
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Error' }
  schemas:
    Error:
      type: object
      required: [error]
      properties:
        error: { type: string }
        message: { type: string }
        details: {}
    Document:
      type: object
      description: JSON document; shapes are listed in modules/OHN-13/data/schema.md
      additionalProperties: true
    Receipt:
      type: object
      required: [receipt_id, committed_at, aggregate_id, aggregate_type, version, status]
      properties:
        receipt_id: { type: string, format: uuid }
        committed_at: { type: string, format: date-time }
        aggregate_id: { type: string, format: uuid }
        aggregate_type: { type: string }
        version: { type: integer, minimum: 1 }
        status: { type: string, enum: [committed, denied, pending] }
    CommandResult:
      type: object
      properties:
        receipt: { $ref: '#/components/schemas/Receipt' }
      additionalProperties: true
    Empty:
      type: object
      additionalProperties: false
    Versioned:
      type: object
      additionalProperties: false
      required: [expected_version]
      properties:
        expected_version: { type: integer, minimum: 1 }
    Export:
      type: object
      additionalProperties: false
      required: [format]
      properties:
        format: { type: string, enum: [csv, pdf] }
        filters: { type: object, additionalProperties: { type: string, maxLength: 512 } }
    Reasoned:
      type: object
      additionalProperties: false
      required: [expected_version, reason]
      properties:
        expected_version: { type: integer, minimum: 1 }
        reason: { type: string, minLength: 10, maxLength: 4096 }
    Content:
      type: object
      additionalProperties: false
      required: [media_type, content_base64]
      properties:
        media_type: { type: string, enum: [application/pdf, text/plain, image/png, image/jpeg, application/fhir+json, application/xml] }
        content_base64: { type: string, minLength: 1, maxLength: 3000000, pattern: "^[A-Za-z0-9+/]+={0,2}$", description: At most 2 MB once decoded }
    Register:
      type: object
      description: OHN-13-F001 Document registry entry
      additionalProperties: false
      required: [patient_ref, source_org, document_type, clinical_date, content_hash, storage_locator, security_label]
      properties:
        patient_ref: { type: string, format: uuid, description: Restricted; never returned }
        source_org: { type: string, format: uuid }
        document_type: { type: string, enum: [discharge_summary, referral_letter, lab_report, imaging_report, prescription, consent_form, operative_note, clinic_letter] }
        clinical_date: { type: string, format: date }
        content_hash: { type: string, maxLength: 512, pattern: "^[0-9a-f]{64}$" }
        storage_locator: { type: string, format: uuid }
        security_label: { type: string, enum: [normal, restricted, very_restricted] }
    Retrieve:
      type: object
      description: OHN-13-F002 Document retrieval request (document_id is the path id)
      additionalProperties: false
      required: [clinical_purpose, relationship_ref, requested_format]
      properties:
        clinical_purpose: { type: string, enum: [treatment, care_coordination, emergency_care] }
        relationship_ref: { type: string, format: uuid }
        requested_format: { type: string, enum: [original, metadata_only] }
    Amendment:
      type: object
      description: OHN-13-F003 Document version correction (document_id is the path id)
      additionalProperties: false
      required: [replacement_ref, change_reason, author_id, effective_at]
      properties:
        replacement_ref: { type: string, format: uuid }
        change_reason: { type: string, minLength: 10, maxLength: 4096 }
        author_id: { type: string, format: uuid }
        effective_at: { type: string, format: date-time }
    QuarantineDecision:
      type: object
      additionalProperties: false
      required: [expected_version, decision, reason]
      properties:
        expected_version: { type: integer, minimum: 1 }
        decision: { type: string, enum: [release, reject] }
        reason: { type: string, minLength: 10, maxLength: 4096 }
    AmendmentDecision:
      type: object
      additionalProperties: false
      required: [expected_version, decision, reason]
      properties:
        expected_version: { type: integer, minimum: 1 }
        decision: { type: string, enum: [verify, reject] }
        reason: { type: string, minLength: 10, maxLength: 4096 }
    SyntheticToken:
      type: object
      additionalProperties: false
      required: [principal_id, role, jurisdiction, organization_id, purpose]
      properties:
        principal_id: { type: string, format: uuid }
        role: { type: string, enum: [clinical_document_steward, licensed_clinician, document_source, unauthorized_viewer, patient] }
        jurisdiction: { type: string, format: uuid }
        organization_id: { type: string, format: uuid }
        purpose: { type: string, enum: [document_management, document_submission, treatment, care_coordination, emergency_care, patient_access] }
        practitioner_id: { type: string, format: uuid, description: Optional OHN-06 health-worker id the gateway attests }
    SyntheticSetup:
      type: object
      additionalProperties: false
      properties:
        deny_patients: { type: array, items: { type: string, format: uuid } }
        deny_labels: { type: array, items: { type: string, enum: [normal, restricted, very_restricted] } }
        down: { type: array, items: { type: string, enum: [OHN-01, OHN-16, OHN-42] } }
        reset: { type: boolean }
