openapi: 3.1.0
info:
  title: OHN-36 Multi-Country Federation and Sovereignty
  version: 1.0.0
  description: >-
    Bilateral federation agreements (W001 establishment, W003 revocation), cross-border disclosure decisions with
    accounting (W002), residency bindings and violations. Every POST requires an Idempotency-Key. Errors use
    {error, message, details}: 401 unauthenticated, 403 policy_denied / separation_of_duties, 404 not disclosed,
    409 stale_version / invalid_transition / downstream_pending, 422 validation and domain refusals, 429 with
    Retry-After, 503 jurisdiction_unavailable / dependency_unavailable. Patient references are never returned.
servers:
  - url: https://opeshealthnetwork.com
security:
  - bearerAuth: []
tags:
  - name: Health
  - name: Synthetic
  - name: Federation
  - name: Agreements
  - name: Disclosure
  - name: Residency
  - name: Harness
  - name: Reports
paths:
  /health/ohn-36:
    get:
      operationId: ohn36_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/federation/synthetic-token:
    post:
      operationId: ohn36_syntheticToken
      summary: Issue a synthetic token (TypeScript baseline, synthetic mode only; the Java module never issues tokens and answers 404)
      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/federation/context:
    get:
      operationId: ohn36_context
      summary: Caller principal and permitted actions
      tags: [Federation]
      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' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/federation/vocabulary:
    get:
      operationId: ohn36_vocabulary
      summary: Controlled vocabularies used by forms
      tags: [Federation]
      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' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/federation/agreements:
    get:
      operationId: ohn36_listAgreements
      x-inventory-id: OHN-36-API-002
      summary: Agreements visible to either country of the pair
      tags: [Agreements]
      parameters:
        - name: state
          in: query
          required: false
          schema: { type: string, maxLength: 64 }
        - name: partner
          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' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
    post:
      operationId: ohn36_proposeAgreement
      summary: Propose a federation agreement (F001)
      tags: [Agreements]
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/AgreementCreate' }
      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/federation/agreements/{id}:
    get:
      operationId: ohn36_getAgreement
      summary: Agreement with history and routing-policy status
      tags: [Agreements]
      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' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/federation/agreements/{id}/submit:
    post:
      operationId: ohn36_submitAgreement
      summary: proposal -> legal_review (origin country)
      tags: [Agreements]
      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/Versioned' }
      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/federation/agreements/{id}/withdraw:
    post:
      operationId: ohn36_withdrawAgreement
      summary: Withdraw before activation (origin country)
      tags: [Agreements]
      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/federation/agreements/{id}/legal-review:
    post:
      operationId: ohn36_legalReview
      summary: legal_review -> technical_attestation or rejected; lawful basis verified with OHN-20
      tags: [Agreements]
      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/LegalReview' }
      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/federation/agreements/{id}/attest:
    post:
      operationId: ohn36_attestAgreement
      summary: technical_attestation -> bilateral_approval; accreditation (OHN-39) and trust anchor (OHN-45) checked
      tags: [Agreements]
      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/Attest' }
      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/federation/agreements/{id}/approve:
    post:
      operationId: ohn36_approveAgreement
      summary: Record the caller country approval (each side once, never the proposer)
      tags: [Agreements]
      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/Versioned' }
      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/federation/agreements/{id}/activate:
    post:
      operationId: ohn36_activateAgreement
      x-inventory-id: OHN-36-API-003
      summary: bilateral_approval -> active; caches the routing policy
      tags: [Agreements]
      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/Versioned' }
      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/federation/agreements/{id}/suspend:
    post:
      operationId: ohn36_suspendAgreement
      summary: active -> suspension_approved; revokes grants and undelivered approvals
      tags: [Agreements]
      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/federation/agreements/{id}/revoke-trust:
    post:
      operationId: ohn36_revokeTrust
      summary: suspension_approved -> trust_keys_revoked (OHN-45); retries a pending confirmation
      tags: [Agreements]
      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/Versioned' }
      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/federation/agreements/{id}/invalidate-sessions:
    post:
      operationId: ohn36_invalidateSessions
      summary: trust_keys_revoked -> sessions_invalidated (OHN-38); retries a pending confirmation
      tags: [Agreements]
      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/Versioned' }
      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/federation/agreements/{id}/close:
    post:
      operationId: ohn36_closeAgreement
      summary: sessions_invalidated -> closed, only when nothing is pending downstream
      tags: [Agreements]
      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/Versioned' }
      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/federation/trust-status:
    get:
      operationId: ohn36_trustStatus
      summary: "Per partner: agreement, accreditation and trust-anchor status"
      tags: [Agreements]
      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' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/federation/topology:
    get:
      operationId: ohn36_topology
      summary: Countries and agreements as a graph
      tags: [Agreements]
      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' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/federation/trust-policies:
    get:
      operationId: ohn36_trustPolicies
      summary: Cached routing policies (CountryTrustPolicy)
      tags: [Agreements]
      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' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/federation/requests:
    get:
      operationId: ohn36_listRequests
      summary: Cross-border requests decided by this country (patient masked)
      tags: [Disclosure]
      parameters:
        - name: state
          in: query
          required: false
          schema: { type: string, maxLength: 64 }
        - name: decision
          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' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
    post:
      operationId: ohn36_submitRequest
      x-inventory-id: OHN-36-API-001
      summary: Decide a cross-border disclosure request (F002, W002); 202 when held for a dependency
      tags: [Disclosure]
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/CrossBorderRequest' }
      responses:
        '201':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '202':
          description: Held (evaluation_pending) because a dependency is unavailable; nothing was disclosed
          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/federation/requests/simulate:
    post:
      operationId: ohn36_simulateRequest
      summary: Evaluate a request without storing it (P004)
      tags: [Disclosure]
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/CrossBorderRequest' }
      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/federation/requests/{id}:
    get:
      operationId: ohn36_getRequest
      summary: Request with decision trace and history (patient masked)
      tags: [Disclosure]
      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' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/federation/requests/{id}/re-evaluate:
    post:
      operationId: ohn36_reEvaluateRequest
      summary: Re-run a held request
      tags: [Disclosure]
      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/Versioned' }
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '202':
          description: Held (evaluation_pending) because a dependency is unavailable; nothing was disclosed
          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/federation/requests/{id}/review:
    post:
      operationId: ohn36_reviewRequest
      summary: Privacy officer review of a sensitive request (not the requester)
      tags: [Disclosure]
      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/RequestReview' }
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '202':
          description: Held (evaluation_pending) because a dependency is unavailable; nothing was disclosed
          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/federation/residency/bindings:
    get:
      operationId: ohn36_listBindings
      summary: Residency bindings of the caller jurisdiction
      tags: [Residency]
      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' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
    post:
      operationId: ohn36_upsertBinding
      summary: Create or update a residency binding (F003)
      tags: [Residency]
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/Binding' }
      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/federation/residency/observations:
    post:
      operationId: ohn36_recordObservation
      summary: Record an observed data placement; 201 when it is a violation
      tags: [Residency]
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/Observation' }
      responses:
        '201':
          description: Success
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        '200':
          description: Placement allowed; no violation recorded
          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/federation/residency/violations:
    get:
      operationId: ohn36_listViolations
      summary: Residency violations
      tags: [Residency]
      parameters:
        - name: mitigated
          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' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/federation/residency/violations/{id}/mitigate:
    post:
      operationId: ohn36_mitigateViolation
      summary: Record mitigation
      tags: [Residency]
      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/Mitigate' }
      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/federation/harness/cases:
    get:
      operationId: ohn36_listHarnessCases
      summary: Cross-border policy test cases (patient hidden)
      tags: [Harness]
      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' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
    post:
      operationId: ohn36_createHarnessCase
      summary: Add a test case with its expected decision
      tags: [Harness]
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/HarnessCase' }
      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/federation/harness/cases/{id}/retire:
    post:
      operationId: ohn36_retireHarnessCase
      summary: Retire a test case
      tags: [Harness]
      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/Versioned' }
      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/federation/harness/run:
    post:
      operationId: ohn36_runHarness
      summary: Run every active case through the evaluator (nothing stored)
      tags: [Harness]
      parameters:
        - $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/federation/reports/{reportId}:
    get:
      operationId: ohn36_runReport
      summary: Run OHN-36-R001..R003
      tags: [Reports]
      parameters:
        - name: reportId
          in: path
          required: true
          schema: { type: string, enum: [OHN-36-R001, OHN-36-R002, OHN-36-R003] }
        - name: origin
          in: query
          required: false
          schema: { type: string, format: uuid }
        - name: destination
          in: query
          required: false
          schema: { type: string, format: uuid }
        - name: status
          in: query
          required: false
          schema: { type: string, maxLength: 64 }
        - name: country_pair
          in: query
          required: false
          schema: { type: string, description: "<origin uuid>,<destination uuid>" }
        - name: jurisdiction
          in: query
          required: false
          schema: { type: string, format: uuid }
        - name: severity
          in: query
          required: false
          schema: { type: string, maxLength: 64 }
        - name: from
          in: query
          required: false
          schema: { type: string, format: date-time }
        - name: to
          in: query
          required: false
          schema: { type: string, format: date-time }
        - 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' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/federation/reports/{reportId}/export:
    post:
      operationId: ohn36_exportReport
      summary: Create a signed, 5-minute, owner-only CSV or PDF export
      tags: [Reports]
      parameters:
        - name: reportId
          in: path
          required: true
          schema: { type: string, enum: [OHN-36-R001, OHN-36-R002, OHN-36-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/federation/exports/{token}:
    get:
      operationId: ohn36_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 } }
            application/pdf: { schema: { type: string, format: binary } }
        '401': { $ref: '#/components/responses/E401' }
        '403': { $ref: '#/components/responses/E403' }
        '404': { $ref: '#/components/responses/E404' }
        '422': { $ref: '#/components/responses/E422' }
        '429': { $ref: '#/components/responses/E429' }
        '503': { $ref: '#/components/responses/E503' }
  /api/ohn/v1/federation/synthetic/setup:
    post:
      operationId: ohn36_syntheticSetup
      summary: Configure the synthetic upstream fakes (synthetic mode only)
      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, already approved, downstream pending or reused idempotency key
      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-36/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' }
        delivery: { type: string, enum: [committed, pending_downstream_confirmation] }
      additionalProperties: true
    Empty:
      type: object
      additionalProperties: false
    Versioned:
      type: object
      additionalProperties: false
      required: [expected_version]
      properties:
        expected_version: { type: integer, minimum: 1 }
    Reasoned:
      type: object
      additionalProperties: false
      required: [expected_version, reason]
      properties:
        expected_version: { type: integer, minimum: 1 }
        reason: { type: string, minLength: 1, maxLength: 2000 }
    AgreementCreate:
      type: object
      description: OHN-36-F001 Federation country agreement
      additionalProperties: false
      required: [origin_country, partner_country, authorized_data_classes, lawful_basis_ref, agreement_version, expiry_date]
      properties:
        origin_country: { type: string, format: uuid, description: Must be the caller jurisdiction }
        partner_country: { type: string, format: uuid }
        authorized_data_classes: { type: string, maxLength: 8192, description: "Comma- or newline-separated record classes: patient_summary, immunization, laboratory, imaging, medication, encounter, mental_health, sexual_reproductive, substance_use, genetic" }
        lawful_basis_ref: { type: string, format: uuid }
        agreement_version: { type: string, minLength: 1, maxLength: 512 }
        expiry_date: { type: string, format: date }
    LegalReview:
      type: object
      additionalProperties: false
      required: [expected_version, decision, reason]
      properties:
        expected_version: { type: integer, minimum: 1 }
        decision: { type: string, enum: [approve, reject] }
        reason: { type: string, minLength: 1, maxLength: 2000 }
    Attest:
      type: object
      additionalProperties: false
      required: [expected_version, attestation_ref, trust_anchor_ref]
      properties:
        expected_version: { type: integer, minimum: 1 }
        attestation_ref: { type: string, format: uuid }
        trust_anchor_ref: { type: string, format: uuid }
    CrossBorderRequest:
      type: object
      description: OHN-36-F002 Cross-border request review
      additionalProperties: false
      required: [requesting_country, source_country, patient_ref, record_class, purpose, legal_authority_ref]
      properties:
        requesting_country: { type: string, format: uuid }
        source_country: { type: string, format: uuid, description: Must be the caller jurisdiction }
        patient_ref: { type: string, format: uuid, description: Restricted; never returned }
        record_class: { type: string, enum: [patient_summary, immunization, laboratory, imaging, medication, encounter, mental_health, sexual_reproductive, substance_use, genetic] }
        purpose: { type: string, enum: [emergency_care, treatment, care_coordination, public_health, legal_obligation] }
        legal_authority_ref: { type: string, format: uuid }
    RequestReview:
      type: object
      additionalProperties: false
      required: [expected_version, decision, reason]
      properties:
        expected_version: { type: integer, minimum: 1 }
        decision: { type: string, enum: [approve, deny] }
        reason: { type: string, minLength: 1, maxLength: 2000 }
    Binding:
      type: object
      description: OHN-36-F003 Residency binding configuration
      additionalProperties: false
      required: [jurisdiction_id, resource_category, primary_region, backup_region, transfer_policy]
      properties:
        jurisdiction_id: { type: string, format: uuid }
        resource_category: { type: string, enum: [clinical_records, identity_data, audit_logs, backups, observability, analytics] }
        primary_region: { type: string, maxLength: 512, pattern: "^[A-Z]{2}-[A-Z0-9]{1,16}$" }
        backup_region: { type: string, maxLength: 512, pattern: "^[A-Z]{2}-[A-Z0-9]{1,16}$" }
        transfer_policy: { type: string, enum: [in_country_only, agreement_required, adequacy_and_agreement] }
        expected_version: { type: integer, minimum: 1, description: Required to update an existing binding }
    Observation:
      type: object
      additionalProperties: false
      required: [resource_category, source_region, destination_region]
      properties:
        resource_category: { type: string, enum: [clinical_records, identity_data, audit_logs, backups, observability, analytics] }
        source_region: { type: string, pattern: "^[A-Z]{2}-[A-Z0-9]{1,16}$" }
        destination_region: { type: string, pattern: "^[A-Z]{2}-[A-Z0-9]{1,16}$" }
        detected_at: { type: string, format: date-time }
    Mitigate:
      type: object
      additionalProperties: false
      required: [expected_version, mitigation]
      properties:
        expected_version: { type: integer, minimum: 1 }
        mitigation: { type: string, minLength: 1, maxLength: 2000 }
    HarnessCase:
      type: object
      additionalProperties: false
      required: [name, input, expected_decision]
      properties:
        name: { type: string, minLength: 1, maxLength: 200 }
        input: { $ref: '#/components/schemas/CrossBorderRequest' }
        expected_decision: { type: string, enum: [approved, denied, review_required] }
    Export:
      type: object
      additionalProperties: false
      required: [format]
      properties:
        format: { type: string, enum: [csv, pdf] }
        filters: { type: object, additionalProperties: { type: string, maxLength: 512 } }
    SyntheticToken:
      type: object
      additionalProperties: false
      required: [principal_id, role, jurisdiction, organization_id, purpose]
      properties:
        principal_id: { type: string, format: uuid }
        role: { type: string, enum: [federation_officer, privacy_officer, federation_gateway, unauthorized_viewer] }
        jurisdiction: { type: string, format: uuid }
        organization_id: { type: string, format: uuid }
        purpose: { type: string, enum: [federation_governance, privacy_oversight, emergency_care, treatment, care_coordination, public_health] }
    SyntheticSetup:
      type: object
      additionalProperties: false
      properties:
        countries: { type: object, additionalProperties: { type: string, pattern: "^[A-Z]{2}$" } }
        unaccredited: { type: array, items: { type: string, format: uuid } }
        revoked_anchors: { type: array, items: { type: string, format: uuid } }
        invalid_authorities: { type: array, items: { type: string, format: uuid } }
        deny_patients: { type: array, items: { type: string, format: uuid } }
        adequacy: { type: array, items: { type: string, pattern: "^[A-Z]{2}$" } }
        down: { type: array, items: { type: string, enum: [OHN-01, OHN-16, OHN-20, OHN-38, OHN-39, OHN-45] } }
        reset: { type: boolean }
