openapi: 3.1.0
info:
  title: usaged
  version: 0.1.0
  description: Fixed-line mediation, redacted source receipts and scoped call itemisation.
security: [{ bearer: [] }]
components:
  securitySchemes: { bearer: { type: http, scheme: bearer } }
  parameters:
    id: { name: id, in: path, required: true, schema: { type: string, format: uuid } }
  responses:
    Clock: {description: Injected service clock, content: {application/json: {schema: {type: object, required: [kind, today], properties: {kind: {const: test}, today: {type: string, format: date}}}}}}
    Ok: { description: Success, content: { application/json: { schema: { type: object } } } }
    Problem: { description: RFC 9457 problem, content: { application/problem+json: { schema: { $ref: '#/components/schemas/Problem' } } } }
  schemas:
    Problem:
      type: object
      required: [status, code, title]
      properties:
        status: { type: integer }
        code: { type: string }
        title: { type: string }
    Date: { type: string, format: date }
    VoiceInput:
      type: object
      additionalProperties: false
      required: [records]
      properties:
        records:
          type: array
          minItems: 1
          maxItems: 1000
          items:
            type: object
            additionalProperties: false
            required: [subscription, call_id, destination, answered_at, seconds]
            properties:
              subscription: { type: string, format: uuid }
              call_id: { type: string, minLength: 1, maxLength: 128 }
              destination: { type: string }
              area_code: { type: string, description: Subscriber area code for local dial strings; optional sixth CSV column }
              answered_at: { type: string, format: date-time }
              seconds: { type: integer, minimum: 0, maximum: 4294967295 }
    VoiceSummary:
      type: object
      required: [id, source, received_on, plan, counts]
      properties:
        id: { type: string, format: uuid }
        source: { type: string }
        received_on: { type: string, format: date }
        plan: { type: string }
        counts: { type: object, additionalProperties: { type: integer, minimum: 1 } }
    VoiceReceipt:
      allOf:
        - { $ref: "#/components/schemas/VoiceSummary" }
        - type: object
          required: [rows]
          properties:
            rows:
              type: array
              items:
                type: object
                required: [row, status, reason]
                properties:
                  row: { type: integer, minimum: 1 }
                  status: { type: string, enum: [accepted, duplicate, held, rejected, suppressed, reconciliation] }
                  reason: { type: string }
    Itemisation:
      type: object
      required: [bill, number, prices, calls]
      properties:
        bill: {type: string, format: uuid}
        number: {type: string}
        prices: {type: string, enum: [gross, net]}
        calls:
          type: array
          minItems: 1
          items:
            type: object
            required: [answered_at, destination, seconds, billed_units, amount, lines]
            properties:
              answered_at: {type: string, description: Answer time including the switch offset}
              destination: {type: string}
              seconds: {type: integer, minimum: 1}
              billed_units: {type: integer, minimum: 0, description: Seconds charged after tariff rounding and allowances}
              amount: {type: string, description: Decimal EUR charge from the sealed rating}
              lines: {type: array, items: {type: integer, minimum: 0}, description: Zero-based positions in the sealed invoice}
    UsageEvent:
      type: object
      required: [id, type, specversion, source, subject, time, data, sequence, businessdate]
      properties:
        id: {type: string, format: uuid}
        type: {const: de.telco.usage.batch-reconciled}
        source: {const: 'urn:telco:usaged'}
        specversion: {const: '1.0'}
        subject: {type: string, description: 'voice-batch/<uuid>'}
        time: {type: string, format: date-time}
        sequence: {type: string}
        businessdate: {type: string, format: date}
        data:
          type: object
          additionalProperties: false
          required: [id]
          properties: {id: {type: string, format: uuid}}
paths:
  /usage/events:
    get:
      operationId: listUsageEvents
      summary: Read completed batch notifications in cursor order
      description: Available to operator and read roles. Caller type patterns still apply.
      parameters:
      - {name: after, in: query, schema: {type: integer, minimum: 0}}
      - {name: limit, in: query, schema: {type: integer, minimum: 1, maximum: 500}}
      - {name: types, in: query, schema: {type: string}}
      - {name: subjects, in: query, schema: {type: string}}
      responses:
        '200':
          description: Oldest events first and the next cursor
          content:
            application/json:
              schema:
                type: object
                required: [events, next]
                properties:
                  events: {type: array, items: {$ref: '#/components/schemas/UsageEvent'}}
                  next: {type: integer}
        default: {$ref: '#/components/responses/Problem'}
  /health:
    get:
      operationId: health
      security: []
      description: "Service identity and process status."
      responses: { "200": { $ref: "#/components/responses/Ok" } }
  /health/live:
    get:
      operationId: healthLive
      security: []
      description: "200 while the process serves. A platform restarts on this one only."
      responses: { "200": { $ref: "#/components/responses/Ok" } }
  /health/ready:
    get:
      operationId: healthReady
      security: []
      description: "200 when the database answers; 503 with fixed reason words (`database: unavailable`) otherwise. A platform routes on this one."
      responses:
        "200": { $ref: "#/components/responses/Ok" }
        "503": { description: "Not ready, with reasons", content: { application/json: { schema: { type: object } } } }
  /openapi.yaml:
    get:
      operationId: getOpenApi
      security: []
      description: This document.
      responses: { "200": { description: The OpenAPI 3.1 document, content: { application/yaml: { schema: { type: string } } } } }
  /clock:
    get:
      operationId: getTestClock
      description: Test clock only.
      responses: { "200": { $ref: "#/components/responses/Clock" }, default: { $ref: "#/components/responses/Problem" } }
    put:
      operationId: setTestClock
      description: Test clock only. Sets the business date and runs retention. If the billing peer is unavailable the date advances and the sweep returns 503. Repeat at the same date to retry.
      requestBody:
        required: true
        content:
          application/json:
            schema: { type: object, additionalProperties: false, required: [today], properties: { today: { $ref: "#/components/schemas/Date" } } }
      responses: { "200": { $ref: "#/components/responses/Clock" }, default: { $ref: "#/components/responses/Problem" } }
  /voice-batches:
    get:
      operationId: listVoiceBatches
      description: Latest 50 completed switch imports. A switch sees only its own source.
      responses:
        "200":
          description: Batch summaries, newest first
          content: { application/json: { schema: { type: array, items: { $ref: "#/components/schemas/VoiceSummary" } } } }
        default: { $ref: "#/components/responses/Problem" }
  /voice-batches/{id}:
    parameters: [{ $ref: "#/components/parameters/id" }]
    get:
      operationId: getVoiceReceipt
      responses:
        "200": { description: Redacted import receipt, content: { application/json: { schema: { $ref: "#/components/schemas/VoiceReceipt" } } } }
        default: { $ref: "#/components/responses/Problem" }
    put:
      operationId: importVoiceBatch
      description: Import up to 1000 outbound call records and 64 KiB. Retry a pending handoff with identical bytes, format, UUID and named switch. An accepted batch survives billing outages.
      requestBody:
        required: true
        content:
          text/csv:
            schema: { type: string }
            example: |
              subscription,call_id,destination,answered_at,seconds
              00000000-0000-0000-0000-000000000001,example-42,+4915100000000,2026-11-01T09:00:00+01:00,61
          application/json:
            schema: { $ref: '#/components/schemas/VoiceInput' }
            example:
              records:
              - { subscription: 00000000-0000-0000-0000-000000000001, call_id: example-42, destination: '+4915100000000', answered_at: '2026-11-01T09:00:00+01:00', seconds: 61 }
      responses:
        "200": { description: Durable row dispositions, content: { application/json: { schema: { $ref: "#/components/schemas/VoiceReceipt" } } } }
        default: { $ref: "#/components/responses/Problem" }
  /evn/preferences/{id}:
    put:
      operationId: setItemisationPreference
      summary: Record the holder's election and written declaration for a subscription
      description: An operator records the archive reference of the holder's text-form declaration. Enabling or changing a choice takes effect next calendar month. Withdrawal takes effect today.
      parameters:
        - {name: id, in: path, required: true, schema: {type: string, format: uuid}}
      requestBody:
        required: true
        content:
          application/json:
            schema:
              oneOf:
                - type: object
                  additionalProperties: false
                  required: [mode]
                  properties:
                    mode: {const: 'off'}
                    include_bundled: {const: false}
                - type: object
                  additionalProperties: false
                  required: [mode, declaration, evidence_ref]
                  properties:
                    mode: {type: string, enum: [shortened, full]}
                    declaration: {type: string, enum: [household_informed, workforce_informed_and_representatives_involved]}
                    evidence_ref: {type: string, format: uuid}
                    include_bundled: {type: boolean, default: false}
      responses:
        '200':
          description: Effective election
          content:
            application/json:
              schema:
                type: object
                required: [mode, effective_on]
                properties:
                  mode: {type: string, enum: ['off', shortened, full]}
                  effective_on: {type: string, format: date}
        '422': {description: Missing or inconsistent declaration}
        '503': {description: Current protection list unavailable}
  /evn/bills/{id}:
    get:
      operationId: getBillItemisation
      summary: Read the current customer's fixed-line call detail for a sealed invoice
      parameters:
        - {name: id, in: path, required: true, schema: {type: string, format: uuid}}
        - {name: format, in: query, schema: {type: string, enum: [json, pdf], default: json}}
      responses:
        '200':
          description: Eligible outbound calls with their charge on the sealed invoice
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Itemisation'}
            application/pdf:
              schema: {type: string, format: binary}
        '404': {description: No available itemisation for this account and invoice}
        '503': {description: Billing facts or current protection list unavailable}
  /evn/purge:
    post:
      operationId: purgeItemisation
      summary: Run a retention sweep using the service clock and authoritative invoice facts
      responses:
        '200':
          description: Number of deleted call details
          content:
            application/json:
              schema:
                type: object
                required: [deleted, classified_deleted, awaiting_dispatch, awaiting_invoice]
                properties:
                  deleted: {type: integer, minimum: 0}
                  classified_deleted: {type: integer, minimum: 0, description: Classified billing records purged by this pass}
                  awaiting_dispatch: {type: integer, minimum: 0, description: Matched call records with no recorded invoice dispatch}
                  awaiting_invoice: {type: integer, minimum: 0, description: Call records with no sealed invoice yet}
        '503': {description: Billing facts unavailable}
