openapi: 3.1.0
info:
  title: OHN-01 Country and Jurisdiction Management
  version: 1.0.0
  description: >
    Written from the observed behaviour of the TypeScript reference (backend/modules/ohn-01, 2026-11-17).
    Reports (/jurisdictions/reports*) and the legacy /auth/token are NOT part of this version: reports are a pending
    gap (docs/program/java/ohn-01-analysis.md); /auth/token is retired (issued tokens to anyone) and replaced by Keycloak.
servers: [{ url: /api/ohn/v1 }]
security: [{ gatewayToken: [] }]
tags: [{ name: jurisdictions }, { name: reports }]
paths:
  /countries:
    get:
      operationId: listCountries
      description: Read by every OHN-01 role with action read, including the read-only service role country_reader (OHN-05 facility codes take the jurisdiction's country_code from here; owner decision 2026-11-23).
      tags: [jurisdictions]
      parameters: [{ $ref: '#/components/parameters/Cursor' }, { $ref: '#/components/parameters/PageSize' }]
      responses:
        '200': { description: Countries, content: { application/json: { schema: { $ref: '#/components/schemas/CountryPage' } } } }
        '401': { $ref: '#/components/responses/Problem' }
        '403': { $ref: '#/components/responses/Problem' }
  /jurisdictions:
    get:
      operationId: listJurisdictionPolicies
      tags: [jurisdictions]
      parameters: [{ $ref: '#/components/parameters/Cursor' }, { $ref: '#/components/parameters/PageSize' }]
      responses:
        '200': { description: Policies, content: { application/json: { schema: { $ref: '#/components/schemas/PolicyPage' } } } }
    post:
      operationId: createCountry
      tags: [jurisdictions]
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/CountryCommand' } } } }
      responses:
        '201': { description: Created, content: { application/json: { schema: { $ref: '#/components/schemas/Country' } } } }
        '409': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
  /jurisdictions/{id}/policies:
    post:
      operationId: publishPolicy
      tags: [jurisdictions]
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/PolicyCommand' } } } }
      responses:
        '201': { description: Published, content: { application/json: { schema: { $ref: '#/components/schemas/Policy' } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
  /jurisdictions/namespace-rules:
    get:
      operationId: listNamespaceRules
      tags: [jurisdictions]
      parameters: [{ name: country_id, in: query, schema: { type: string, format: uuid } }]
      responses:
        '200': { description: Rules newest first (capped at 500), content: { application/json: { schema: { $ref: '#/components/schemas/NamespaceRuleList' } } } }
        '422': { $ref: '#/components/responses/Problem' }
    post:
      operationId: createNamespaceRule
      tags: [jurisdictions]
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/NamespaceRuleCommand' } } } }
      responses:
        '201': { description: Created, content: { application/json: { schema: { $ref: '#/components/schemas/NamespaceRule' } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
  /jurisdictions/namespace-rules/{ruleId}:
    get:
      operationId: getNamespaceRule
      tags: [jurisdictions]
      parameters: [{ name: ruleId, in: path, required: true, schema: { type: string } }]
      responses:
        '200': { description: Rule with overlapping rules, content: { application/json: { schema: { $ref: '#/components/schemas/NamespaceRuleDetail' } } } }
        '404': { $ref: '#/components/responses/Problem' }
  /jurisdictions/{id}/activate:
    post:
      operationId: activateCountry
      tags: [jurisdictions]
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      responses:
        '200': { description: Activated, content: { application/json: { schema: { $ref: '#/components/schemas/Country' } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
  /jurisdictions/{id}/suspend:
    post:
      operationId: suspendCountryLegacy
      summary: Legacy one-step route; now only records a suspension request (dual control)
      tags: [jurisdictions]
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/SuspendCommand' } } } }
      responses:
        '200': { description: Suspension requested, content: { application/json: { schema: { $ref: '#/components/schemas/Country' } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '409': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
  /jurisdictions/{id}/reinstate:
    post:
      operationId: reinstateCountry
      tags: [jurisdictions]
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/ReasonCommand' } } } }
      responses:
        '200': { description: Reinstated, content: { application/json: { schema: { $ref: '#/components/schemas/Country' } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
  /jurisdictions/{id}/history:
    get:
      operationId: getCountryHistory
      tags: [jurisdictions]
      parameters: [{ $ref: '#/components/parameters/Id' }]
      responses:
        '200': { description: Audit trail oldest first, content: { application/json: { schema: { $ref: '#/components/schemas/HistoryList' } } } }
        '404': { $ref: '#/components/responses/Problem' }
  /jurisdictions/{id}/suspension-request:
    get:
      operationId: getSuspensionRequest
      tags: [jurisdictions]
      parameters: [{ $ref: '#/components/parameters/Id' }]
      responses:
        '200': { description: State, content: { application/json: { schema: { $ref: '#/components/schemas/SuspensionState' } } } }
        '404': { $ref: '#/components/responses/Problem' }
    post:
      operationId: requestSuspension
      tags: [jurisdictions]
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/ReasonCommand' } } } }
      responses:
        '200': { description: Requested, content: { application/json: { schema: { $ref: '#/components/schemas/Country' } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
  /jurisdictions/{id}/suspension-request/approve:
    post:
      operationId: approveSuspension
      tags: [jurisdictions]
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/ReasonCommand' } } } }
      responses:
        '200': { description: Suspended, content: { application/json: { schema: { $ref: '#/components/schemas/Country' } } } }
        '403': { $ref: '#/components/responses/Problem' }
        '409': { $ref: '#/components/responses/Problem' }
  /jurisdictions/{id}/suspension-request/withdraw:
    post:
      operationId: withdrawSuspension
      tags: [jurisdictions]
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/ReasonCommand' } } } }
      responses:
        '200': { description: Withdrawn or refused (back to active), content: { application/json: { schema: { $ref: '#/components/schemas/Country' } } } }
        '409': { $ref: '#/components/responses/Problem' }
  /jurisdictions/reports:
    get:
      operationId: listReports
      tags: [reports]
      responses:
        '200': { description: Available reports, content: { application/json: { schema: { $ref: '#/components/schemas/ReportList' } } } }
        '403': { $ref: '#/components/responses/Problem' }
  /jurisdictions/reports/{reportId}:
    get:
      operationId: runReport
      tags: [reports]
      description: Filters are strict; an unknown query parameter is refused with 422. Scope comes from the token jurisdiction claim (country id or *).
      parameters:
        - { name: reportId, in: path, required: true, schema: { type: string } }
        - { name: jurisdiction, in: query, schema: { type: string } }
        - { name: status, in: query, schema: { type: string } }
        - { name: policy_domain, in: query, schema: { type: string } }
        - { name: issuer, in: query, schema: { type: string } }
        - { name: actor, in: query, schema: { type: string } }
        - { name: date, in: query, schema: { type: string } }
        - { name: period_from, in: query, schema: { type: string } }
        - { name: period_to, in: query, schema: { type: string } }
        - { name: cursor, in: query, schema: { type: string } }
      responses:
        '200': { description: Report, content: { application/json: { schema: { $ref: '#/components/schemas/Report' } } } }
        '403': { $ref: '#/components/responses/Problem' }
        '404': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
  /jurisdictions/reports/{reportId}/export:
    post:
      operationId: exportReport
      tags: [reports]
      parameters: [{ name: reportId, in: path, required: true, schema: { type: string } }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/ExportCommand' } } } }
      responses:
        '201': { description: Export created - single use and valid for five minutes, content: { application/json: { schema: { $ref: '#/components/schemas/ExportReceipt' } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
  /jurisdictions/report-exports/{token}:
    get:
      operationId: downloadReportExport
      tags: [reports]
      parameters: [{ name: token, in: path, required: true, schema: { type: string } }]
      responses:
        '200': { description: The file as csv or pdf, content: { text/csv: { schema: { type: string, format: binary } }, application/pdf: { schema: { type: string, format: binary } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '410': { $ref: '#/components/responses/Problem' }

components:
  securitySchemes:
    gatewayToken: { type: http, scheme: bearer, bearerFormat: JWT, description: ES256 token issued by the OHN gateway (audience ohn-01) }
  parameters:
    Id: { name: id, in: path, required: true, schema: { type: string } }
    Cursor: { name: cursor, in: query, schema: { type: string } }
    PageSize: { name: page_size, in: query, schema: { type: integer, minimum: 1, maximum: 100, default: 25 } }
    IdempotencyKey: { name: Idempotency-Key, in: header, required: true, schema: { type: string, maxLength: 512 } }
  responses:
    Problem:
      description: application/problem+json with the legacy `error` code
      content: { application/problem+json: { schema: { $ref: '#/components/schemas/Problem' } } }
  schemas:
    Problem:
      type: object
      required: [error, message]
      properties:
        type: { type: string }
        title: { type: string }
        status: { type: integer }
        detail: { type: string }
        error: { type: string, description: 'unauthenticated | denied_by_policy | not_found | conflict | domain_validation_failed | missing_idempotency_key | idempotency_conflict | request_in_progress | no_pending_request | second_approver_required | payload_too_large | server_unavailable' }
        message: { type: string }
        trace_id: { type: string }
        details: { type: object, additionalProperties: true }
    CountryStatus: { type: string, enum: [draft, reviewed, approved, active, suspension_requested, suspended, reinstated, superseded] }
    Country:
      type: object
      required: [id, country_code, country_name, default_locale, time_zone, issuing_authority, status, version, created_at, updated_at]
      properties:
        id: { type: string, format: uuid }
        country_code: { type: string }
        country_name: { type: string }
        default_locale: { type: string }
        time_zone: { type: string }
        issuing_authority: { type: string, format: uuid }
        activation_date: { type: [string, 'null'], format: date-time }
        status: { $ref: '#/components/schemas/CountryStatus' }
        version: { type: integer }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
    CountryCommand:
      type: object
      required: [country_code, country_name, default_locale, time_zone, issuing_authority]
      properties:
        country_code: { type: string, maxLength: 128, pattern: '^[A-Za-z0-9._:/-]+$' }
        country_name: { type: string, maxLength: 512 }
        default_locale: { type: string, maxLength: 32, pattern: '^[a-z]{2}(-[A-Z]{2})?$' }
        time_zone: { type: string, maxLength: 512 }
        issuing_authority: { type: string, format: uuid }
        activation_date: { type: string, format: date-time }
    CountryPage:
      type: object
      required: [data, next_cursor, page_size]
      properties:
        data: { type: array, items: { $ref: '#/components/schemas/Country' } }
        next_cursor: { type: [string, 'null'] }
        page_size: { type: integer }
    Policy:
      type: object
      properties:
        id: { type: string, format: uuid }
        country_id: { type: string, format: uuid }
        policy_domain: { type: string }
        policy_version: { type: string }
        effective_from: { type: string, format: date-time }
        policy_document: { type: string }
        reviewer_id: { type: string, format: uuid }
        reason: { type: string }
        status: { type: string }
        version: { type: integer }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
    PolicyCommand:
      type: object
      required: [jurisdiction_id, policy_domain, policy_version, effective_from, policy_document, reviewer_id, reason]
      properties:
        jurisdiction_id: { type: string, format: uuid }
        policy_domain: { type: string, maxLength: 128 }
        policy_version: { type: string, maxLength: 512 }
        effective_from: { type: string, format: date-time }
        policy_document: { type: string, minLength: 1 }
        reviewer_id: { type: string, format: uuid }
        reason: { type: string, minLength: 10, maxLength: 4096 }
    PolicyPage:
      type: object
      properties:
        data: { type: array, items: { $ref: '#/components/schemas/Policy' } }
        next_cursor: { type: [string, 'null'] }
        page_size: { type: integer }
    NamespaceRule:
      type: object
      properties:
        id: { type: string, format: uuid }
        country_id: { type: string, format: uuid }
        identifier_namespace: { type: string }
        issuer_id: { type: string, format: uuid }
        format_pattern: { type: string }
        uniqueness_scope: { type: string, enum: [country, jurisdiction, facility] }
        valid_from: { type: string, format: date }
        version: { type: integer }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
    NamespaceRuleCommand:
      type: object
      required: [country_id, identifier_namespace, issuer_id, format_pattern, uniqueness_scope, valid_from]
      properties:
        country_id: { type: string, format: uuid }
        identifier_namespace: { type: string, maxLength: 128, pattern: '^[A-Za-z0-9._:/-]+$' }
        issuer_id: { type: string, format: uuid }
        format_pattern: { type: string, maxLength: 512 }
        uniqueness_scope: { type: string, enum: [country, jurisdiction, facility] }
        valid_from: { type: string, format: date }
    NamespaceRuleList:
      type: object
      properties: { data: { type: array, items: { $ref: '#/components/schemas/NamespaceRule' } } }
    NamespaceRuleDetail:
      allOf:
        - { $ref: '#/components/schemas/NamespaceRule' }
        - { type: object, properties: { overlapping_rules: { type: array, items: { $ref: '#/components/schemas/NamespaceRule' } } } }
    SuspendCommand:
      type: object
      required: [reason, second_approver_id]
      properties:
        reason: { type: string, minLength: 10, maxLength: 4096 }
        second_approver_id: { type: string, format: uuid }
    ReasonCommand:
      type: object
      required: [reason]
      properties: { reason: { type: string, minLength: 10, maxLength: 4096 } }
    ReportList:
      type: object
      properties: { reports: { type: array, items: { type: object, properties: { id: { type: string }, title: { type: string } } } } }
    Report:
      type: object
      required: [report_id, title, columns, rows, summary, state, generated_at, next_cursor]
      properties:
        report_id: { type: string }
        title: { type: string }
        columns: { type: array, items: { type: string } }
        rows: { type: array, items: { type: object, additionalProperties: true } }
        summary: { type: object, additionalProperties: true }
        state: { type: string, enum: [rendered, no_authorized_data] }
        generated_at: { type: string, format: date-time }
        next_cursor: { type: [string, 'null'] }
    ExportCommand:
      type: object
      required: [format]
      properties:
        format: { type: string, enum: [csv, pdf] }
        filters: { type: object, additionalProperties: true }
    ExportReceipt:
      type: object
      properties:
        export_id: { type: string, format: uuid }
        report_id: { type: string }
        format: { type: string }
        rows: { type: integer }
        expires_at: { type: string, format: date-time }
        single_use: { type: boolean }
        download_path: { type: string }
    HistoryEntry:
      type: object
      properties:
        id: { type: string, format: uuid }
        event_type: { type: string }
        at: { type: string, format: date-time }
        status: { type: [string, 'null'] }
        previous_status: { type: [string, 'null'] }
        policy_domain: { type: [string, 'null'] }
        policy_version: { type: [string, 'null'] }
        reason: { type: [string, 'null'] }
        actor: { type: [string, 'null'] }
        requested_by: { type: [string, 'null'] }
        decision: { type: [string, 'null'] }
    HistoryList:
      type: object
      properties: { data: { type: array, items: { $ref: '#/components/schemas/HistoryEntry' } } }
    SuspensionState:
      type: object
      properties:
        country_id: { type: string, format: uuid }
        status: { $ref: '#/components/schemas/CountryStatus' }
        pending:
          type: [object, 'null']
          properties: { requested_by: { type: string }, reason: { type: string }, at: { type: string, format: date-time } }
