openapi: 3.1.0
info:
  title: bhub Operator API
  version: 1.0.0
  summary: Calls the operator makes to bhub (launch, catalog, rounds, feeds, reports, webhooks).
  description: |
    ## Conventions
    - Transport: HTTPS, JSON (`application/json; charset=utf-8`), UTF-8. Feeds and the daily
      report also answer `text/csv` when asked with `Accept: text/csv`.
    - Guides: https://bhubsystems.com/docs (this contract: docs/operator-api.md).
    - Amounts: decimal strings in the currency's minor-unit precision (`"12.50"` ZAR,
      `"1500"` UGX, `"0.125"` TND), never floats. The precision table is `GET /v1/currencies`.
    - Identifiers: at most 64 characters, `[A-Za-z0-9._:-]`. bhub ids are UUIDs.
    - Times: RFC 3339 UTC with `Z`. Business days (`date`) are in the jurisdiction's display
      time zone (SAST for ZA).
    - One tenant = one website: a key belongs to exactly one tenant and sees only its data.

    ## Signing (HMAC-SHA256, per-tenant API key)
    API keys are made by the tenant's admin on the portal (Team and keys, "Operator API"):
    a key id (`opk_` + 24 hex) and a secret (64 hex characters, shown once). They are separate
    from the wallet signing keys. At most two keys are active, so you can rotate without a gap.
    Every request except the public ones (`/v1/health`, `/v1/currencies`, `/v1/webhooks/keys`)
    carries:
    - `X-Bhub-Key-Id`: the key id.
    - `X-Bhub-Timestamp`: Unix seconds. Outside +/-300 s of bhub's clock: `STALE_TIMESTAMP`.
    - `X-Bhub-Signature`: lowercase hex HMAC-SHA256, keyed with the secret's 64 characters, of
      `timestamp + "\n" + METHOD + "\n" + path_and_query + "\n" + raw_body`, where
      `path_and_query` is exactly as sent (`/v1/games?limit=100`) and `raw_body` is empty for GET.
    Every answer to a signed request is signed back with the same key: `X-Bhub-Signature` =
    hex HMAC-SHA256 of `timestamp + "\n" + raw_response_body`.
    A signed POST is accepted once: the identical signed request again is `REPLAYED_REQUEST`.

    ## Allowed addresses
    Requests are accepted only from the tenant's allowed addresses (single IPs or CIDR ranges,
    set on the go-live checklist). No addresses set means no calls are accepted.

    ## Rate limits (token bucket per key)
    - API: 50 requests/second sustained, bursts of 100.
    - Feeds and reports: 5 requests/second, bursts of 10 (page with `limit` up to 1000 instead).
    Every signed answer carries `X-RateLimit-Limit` and `X-RateLimit-Remaining`; over the limit
    is HTTP 429 `RATE_LIMITED` with `Retry-After` (seconds).

    ## Errors
    HTTP 4xx/5xx with `{"error": CODE, "message": "plain English", "retryable": bool,
    "details": {...}}`. The code list is closed (`ErrorCode`). Retry only when `retryable` is
    true (`RATE_LIMITED`, `WALLET_UNAVAILABLE`, `SERVICE_UNAVAILABLE`, `INTERNAL_RETRY`);
    re-sign every retry.

    ## Feeds (timepoint cursors)
    Feeds list rows in the order they became durable. A cursor is an opaque position; resume
    from the last `next_cursor` at any time, it never expires and never skips a row (a row is
    served only after every transaction that started before it has finished). Rows that change
    (a round closing, a retry giving up) appear again later with their new state: keep the
    latest per id. `has_more: false` means you are at the head: poll again later from the same
    cursor. CSV answers carry `X-Bhub-Next-Cursor` and `X-Bhub-Has-More` headers.

    ## Webhooks
    bhub POSTs events to the tenant's message address (https only, set on Team and keys),
    signed with Ed25519: `X-Bhub-Signature` = base64url (no padding) signature of
    `X-Bhub-Timestamp + "\n" + raw_body`, by the key `X-Bhub-Key-Id` published at
    `GET /v1/webhooks/keys`. Answer 2xx within 10 s. Otherwise bhub retries after 1 min, 5 min,
    15 min, 1 h, 2 h, 4 h, 8 h, 8 h, then gives up (about 24 h). Deliveries may repeat:
    deduplicate on `X-Bhub-Webhook-Id` (= `event_id`).
servers:
  - url: https://api.bhts.dev
    description: Production
  - url: https://sandbox.bhts.dev
    description: Sandbox (simulator wallets and test-only games)
security:
  - bhubKey: []
    bhubTimestamp: []
    bhubSignature: []

paths:
  /v1/health:
    get:
      operationId: health
      security: []
      summary: Liveness and dependencies. Unsigned.
      responses:
        '200': { description: Up, content: { application/json: { schema: { $ref: '#/components/schemas/Health' } } } }
        '503': { description: Degraded, content: { application/json: { schema: { $ref: '#/components/schemas/Health' } } } }
  /v1/currencies:
    get:
      operationId: currencies
      security: []
      summary: Supported currencies and their minor-unit precision. Unsigned.
      responses:
        '200':
          description: Currencies
          content:
            application/json:
              schema:
                type: object
                required: [currencies]
                properties:
                  currencies: { type: array, items: { $ref: '#/components/schemas/Currency' } }
  /v1/webhooks/keys:
    get:
      operationId: webhookKeys
      security: []
      summary: Public keys that verify webhook deliveries (current, and previous during rotation). Unsigned; cache 5 min.
      responses:
        '200':
          description: Keys
          content:
            application/json:
              schema:
                type: object
                required: [keys]
                properties:
                  keys: { type: array, items: { $ref: '#/components/schemas/WebhookKey' } }

  /v1/launch:
    post:
      operationId: launch
      summary: Get a launch URL for a game, real money or demo.
      description: |
        `real` exchanges your launch `token` with your wallet (`authenticate`, seamless-wallet
        contract) and opens a game session; the wallet must answer for `player_id` in `currency`.
        `demo` never touches the wallet; every demo launch is logged (with `player_id` when you give
        one), the studio's reports add its rounds and free spins, and the counts appear in
        `GET /v1/reports/daily-totals` (`demo`). Demo play raises no significant event.
        A free-to-play account or game launches in `demo` only (`details.reason: free_to_play`).
        Games the tenant cannot offer are refused with
        `GAME_UNAVAILABLE` and `details.reason`: `hidden` (you hid it), `stopped` (a regulated
        change awaits the board), `not_certified` (not certified in your jurisdiction),
        `not_enabled` (studio has not enabled it), `test_only` (real money outside the sandbox),
        `account_not_live`, `free_to_play`, `studio_not_connected` (you have no active connection with
        the studio: add your studio account on the portal's Studio connections screen; bhub verifies
        and activates it; there is no fallback to any other account), `not_in_contract` (your
        connection does not enable this game) and `studio_currency` (your connection does not cover
        `currency`). The launch URL is valid until `expires_at`
        and opens one session.
      requestBody:
        required: true
        content: { application/json: { schema: { $ref: '#/components/schemas/LaunchRequest' } } }
      responses:
        '201': { description: Launch URL, content: { application/json: { schema: { $ref: '#/components/schemas/LaunchResponse' } } } }
        '400': { $ref: '#/components/responses/Error' }
        '401': { $ref: '#/components/responses/Error' }
        '403': { $ref: '#/components/responses/Error' }
        '404': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }

  /v1/games:
    get:
      operationId: listGames
      summary: The tenant's game catalog with availability, certification and studio facts.
      parameters:
        - { name: limit, in: query, schema: { type: integer, minimum: 1, maximum: 500, default: 100 } }
        - { name: cursor, in: query, schema: { type: string } }
        - { name: updated_since, in: query, description: Only games changed at or after this time (catalog sync)., schema: { type: string, format: date-time } }
      responses:
        '200':
          description: One page, ordered by game_id.
          content:
            application/json:
              schema:
                type: object
                required: [games, next_cursor, jurisdiction, currencies, as_of]
                properties:
                  games: { type: array, items: { $ref: '#/components/schemas/Game' } }
                  next_cursor: { type: [string, 'null'] }
                  jurisdiction: { type: string }
                  currencies: { type: array, items: { type: string } }
                  as_of: { type: string, format: date-time }
        '400': { $ref: '#/components/responses/Error' }
  /v1/games/{game_id}:
    get:
      operationId: getGame
      parameters: [ { $ref: '#/components/parameters/GameId' } ]
      responses:
        '200': { description: Game, content: { application/json: { schema: { $ref: '#/components/schemas/Game' } } } }
        '404': { $ref: '#/components/responses/Error' }
  /v1/games/{game_id}/free-spin-levels:
    get:
      operationId: freeSpinLevels
      summary: The studio's free spin bet levels per currency (read only; bhub runs no campaigns).
      description: |
        Use these values when you grant free spins in the studio's back office. A currency the
        studio does not cover is listed with `missing: true` and no levels; it is never
        converted.
      parameters:
        - { $ref: '#/components/parameters/GameId' }
        - { name: currency, in: query, description: Comma-separated; default your currencies., schema: { type: string } }
      responses:
        '200': { description: Levels, content: { application/json: { schema: { $ref: '#/components/schemas/FreeSpinLevels' } } } }
        '404': { $ref: '#/components/responses/Error' }
  /v1/assets/manifest:
    get:
      operationId: assetManifest
      summary: Artwork per game (thumbnail, banner, icon, background; webp/avif) on the CDN.
      parameters:
        - { name: game_id, in: query, schema: { type: string, format: uuid } }
        - { name: updated_since, in: query, schema: { type: string, format: date-time } }
      responses:
        '200': { description: Manifest, content: { application/json: { schema: { $ref: '#/components/schemas/AssetManifest' } } } }

  /v1/rounds/{round_id}:
    get:
      operationId: getRound
      summary: Round information (the bet slip) with its transactions.
      parameters: [ { $ref: '#/components/parameters/RoundId' } ]
      responses:
        '200': { description: Round, content: { application/json: { schema: { $ref: '#/components/schemas/Round' } } } }
        '404': { $ref: '#/components/responses/Error' }
  /v1/rounds/by-provider-round/{provider_round_id}:
    get:
      operationId: getRoundByProviderRound
      summary: The same, looked up by the studio's round id.
      parameters:
        - { name: provider_round_id, in: path, required: true, schema: { type: string, maxLength: 64 } }
        - { name: studio, in: query, description: Studio code; needed only when two studios use the same id (AMBIGUOUS_ROUND)., schema: { type: string } }
      responses:
        '200': { description: Round, content: { application/json: { schema: { $ref: '#/components/schemas/Round' } } } }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }
  /v1/rounds/{round_id}/close:
    post:
      operationId: forceCloseRound
      summary: Force-close an open round, per the studio's policy.
      description: |
        Recorded as a significant event (RESULT_AMENDED) and in the audit trail, and sent as
        the `round.force_closed` webhook. The studio's policy decides:
        - `close_as_is`: the round closes with what was booked.
        - `refund_stake`: when the round has no wins, every stake not yet reversed is returned
          to the player as a `rollback` (same path as bhub's stuck-round sweeper): it shows in the
          round's transactions at once, is sent to your wallet through bhub's durable credit
          retry queue (`payout.failed` / `payout.manual` if your wallet does not confirm), and the
          round ends `cancelled`. A round with wins closes as is (stakes are never returned on
          top of wins). A later rollback of the same bet by the studio is a no-op, so the stake
          is never returned twice.
        - `not_allowed`: 409 `FORCE_CLOSE_NOT_ALLOWED`; the studio settles its own rounds.
      parameters: [ { $ref: '#/components/parameters/RoundId' } ]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [reason]
              additionalProperties: false
              properties:
                reason: { type: string, minLength: 3, maxLength: 500 }
      responses:
        '200': { description: Closed round, content: { application/json: { schema: { $ref: '#/components/schemas/Round' } } } }
        '404': { $ref: '#/components/responses/Error' }
        '409': { $ref: '#/components/responses/Error' }

  /v1/feeds/transactions:
    get:
      operationId: feedTransactions
      summary: Every money movement (bet, win, rollback, adjustment...), in durable order.
      parameters: [ { $ref: '#/components/parameters/Cursor' }, { $ref: '#/components/parameters/Since' }, { $ref: '#/components/parameters/FeedLimit' } ]
      responses:
        '200':
          description: Page
          headers: { $ref: '#/components/headers/FeedHeaders' }
          content:
            application/json: { schema: { $ref: '#/components/schemas/TransactionPage' } }
            text/csv: { schema: { type: string, description: 'Header row then one line per item, same fields as JSON.' } }
        '400': { $ref: '#/components/responses/Error' }
        '406': { $ref: '#/components/responses/Error' }
  /v1/feeds/rounds:
    get:
      operationId: feedRounds
      summary: Rounds, again each time one changes (closes, is adjusted). Keep the latest per round_id.
      parameters: [ { $ref: '#/components/parameters/Cursor' }, { $ref: '#/components/parameters/Since' }, { $ref: '#/components/parameters/FeedLimit' } ]
      responses:
        '200':
          description: Page
          headers: { $ref: '#/components/headers/FeedHeaders' }
          content:
            application/json: { schema: { $ref: '#/components/schemas/RoundFeedPage' } }
            text/csv: { schema: { type: string } }
  /v1/feeds/failed-transactions:
    get:
      operationId: feedFailedTransactions
      summary: Credits your wallet has not confirmed, with their retry state (pending, done, manual).
      description: |
        `pending`: bhub is retrying (1 min, 10 min, then hourly) until `give_up_at` (72 h after
        the first failure). `done`: delivered late. `manual`: bhub gave up; settle it by hand
        (also sent as the `payout.manual` webhook).
      parameters: [ { $ref: '#/components/parameters/Cursor' }, { $ref: '#/components/parameters/Since' }, { $ref: '#/components/parameters/FeedLimit' } ]
      responses:
        '200':
          description: Page
          headers: { $ref: '#/components/headers/FeedHeaders' }
          content:
            application/json: { schema: { $ref: '#/components/schemas/FailedPage' } }
            text/csv: { schema: { type: string } }
  /v1/reports/daily-totals:
    get:
      operationId: dailyTotals
      summary: One business day's totals per studio, game and currency, from bhub's meters. Test-only games excluded. JSON also lists demo play per game.
      parameters:
        - { name: date, in: query, required: true, description: Business day in your jurisdiction's time zone., schema: { type: string, format: date } }
      responses:
        '200':
          description: Totals
          content:
            application/json: { schema: { $ref: '#/components/schemas/DailyTotals' } }
            text/csv: { schema: { type: string } }
        '400': { $ref: '#/components/responses/Error' }

components:
  securitySchemes:
    bhubKey: { type: apiKey, in: header, name: X-Bhub-Key-Id }
    bhubTimestamp: { type: apiKey, in: header, name: X-Bhub-Timestamp }
    bhubSignature: { type: apiKey, in: header, name: X-Bhub-Signature, description: 'hex HMAC-SHA256 of ts\nMETHOD\npath_and_query\nbody' }

  parameters:
    GameId: { name: game_id, in: path, required: true, schema: { type: string, format: uuid } }
    RoundId: { name: round_id, in: path, required: true, schema: { type: string, format: uuid } }
    Cursor: { name: cursor, in: query, description: 'next_cursor from the previous page.', schema: { type: string } }
    Since: { name: since, in: query, description: 'First request only: start at rows recorded at or after this time. Without it the feed starts at the oldest row kept online.', schema: { type: string, format: date-time } }
    FeedLimit: { name: limit, in: query, schema: { type: integer, minimum: 1, maximum: 1000, default: 500 } }

  headers:
    FeedHeaders:
      description: 'X-Bhub-Next-Cursor and X-Bhub-Has-More (also on JSON answers).'
      schema: { type: string }

  responses:
    Error:
      description: Error
      content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } }

  schemas:
    BetSlip:
      type: object
      description: "Electronic bet slip (SANS 1718-4 6.10.2). Operator, licence and outlet details are those on file when the ticket was issued."
      required: [ticket_no, ticket_display, operator, legal_entity, licence_no, outlet, province, terminal, date, time, time_zone, stake, wager_type, account_no, event, selection, result, payout, free_text]
      properties:
        ticket_no: { type: [string, 'null'], description: "Wire form, e.g. ZA-0009843601220. Null until numbered (seconds after the round commits) and always null for sandbox tenants." }
        ticket_display: { type: [string, 'null'], description: "Grouped for printing, e.g. ZA-0009 843 601 220." }
        operator: { type: string }
        legal_entity: { type: string }
        licence_no: { type: [string, 'null'] }
        outlet: { type: [string, 'null'], description: The licensed site (venue). }
        province: { type: [string, 'null'] }
        terminal: { type: string, description: The player's platform. }
        date: { type: string, format: date, description: "Round opened, jurisdiction local time." }
        time: { type: string, description: "HH:MM:SS, jurisdiction local time." }
        time_zone: { type: string, description: "e.g. SAST" }
        stake: { $ref: '#/components/schemas/Amount' }
        wager_type: { type: string, description: "Game category, with _free_spin appended for free rounds." }
        account_no: { type: string, description: The player reference. }
        event:
          type: object
          properties: { game_id: { type: string }, game_name: { type: string }, studio: { type: string }, config_version: { type: [string, 'null'] }, round_id: { type: string }, provider_round_id: { type: string }, bonus_ref: { type: [string, 'null'] } }
        selection: { type: string, enum: [game_outcome] }
        result: { type: string, enum: [open, win, no_win, cancelled] }
        payout: { $ref: '#/components/schemas/Amount' }
        free_text: { type: string, maxLength: 500, description: "The operator's ticket text, else a line naming the game, studio and studio round." }
    Amount: { type: string, pattern: '^-?[0-9]+(\.[0-9]+)?$', description: 'Decimal string at the currency precision.' }
    Error:
      type: object
      required: [error, message, retryable]
      properties:
        error: { $ref: '#/components/schemas/ErrorCode' }
        message: { type: string }
        retryable: { type: boolean }
        details: { type: object }
    ErrorCode:
      type: string
      description: |
        Non-retryable: BAD_REQUEST, BAD_CURSOR, INVALID_SIGNATURE, STALE_TIMESTAMP,
        REPLAYED_REQUEST, IP_NOT_ALLOWED, NOT_FOUND, GAME_NOT_FOUND, ROUND_NOT_FOUND,
        GAME_UNAVAILABLE, CURRENCY_NOT_SUPPORTED, LAUNCH_REFUSED, AMBIGUOUS_ROUND, ROUND_NOT_OPEN,
        FORCE_CLOSE_NOT_ALLOWED, NOT_ACCEPTABLE.
        Retryable: RATE_LIMITED, WALLET_UNAVAILABLE, SERVICE_UNAVAILABLE, INTERNAL_RETRY.
      enum: [BAD_REQUEST, BAD_CURSOR, INVALID_SIGNATURE, STALE_TIMESTAMP, REPLAYED_REQUEST, IP_NOT_ALLOWED,
             NOT_FOUND, GAME_NOT_FOUND, ROUND_NOT_FOUND, GAME_UNAVAILABLE, CURRENCY_NOT_SUPPORTED, LAUNCH_REFUSED,
             AMBIGUOUS_ROUND, ROUND_NOT_OPEN, FORCE_CLOSE_NOT_ALLOWED, NOT_ACCEPTABLE,
             RATE_LIMITED, WALLET_UNAVAILABLE, SERVICE_UNAVAILABLE, INTERNAL_RETRY]

    Health:
      type: object
      required: [status, time, version, components]
      properties:
        status: { type: string, enum: [ok, degraded] }
        time: { type: string, format: date-time }
        version: { type: string }
        components: { type: object, additionalProperties: { type: string } }
    Currency:
      type: object
      required: [code, exponent, name, example, minor_unit]
      properties:
        code: { type: string, pattern: '^[A-Z]{3}$' }
        exponent: { type: integer, minimum: 0, maximum: 3 }
        name: { type: string }
        example: { type: string, examples: ['12.50'] }
        minor_unit: { type: string, examples: ['0.01'] }
    WebhookKey:
      type: object
      required: [key_id, alg, status, kty, crv, x, public_key_hex]
      properties:
        key_id: { type: string }
        alg: { type: string, const: EdDSA }
        status: { type: string, enum: [current, previous] }
        kty: { type: string, const: OKP }
        crv: { type: string, const: Ed25519 }
        x: { type: string, description: 'base64url public key (RFC 8037 JWK)' }
        public_key_hex: { type: string }

    LaunchRequest:
      type: object
      required: [mode, game_id, currency, language]
      additionalProperties: false
      properties:
        mode: { type: string, enum: [real, demo] }
        game_id: { type: string, format: uuid }
        player_id: { type: string, maxLength: 64, description: "Required for real. Optional for demo: your player reference, logged with the demo session." }
        token: { type: string, maxLength: 256, description: 'Real: your launch token, sent to your wallet''s authenticate.' }
        currency: { type: string, pattern: '^[A-Z]{3}$' }
        language: { type: string, examples: [en, en-ZA, sw] }
        platform: { type: string, enum: [desktop, mobile], default: desktop }
        lobby_url: { type: string, format: uri }
        cashier_url: { type: string, format: uri }
        history_url: { type: string, format: uri }
        reality_check:
          type: object
          properties:
            interval_minutes: { type: integer, minimum: 1, maximum: 1440 }
            elapsed_minutes: { type: integer, minimum: 0 }
        ip_address: { type: string, description: "The player's address, passed to the studio." }
    LaunchResponse:
      type: object
      required: [launch_id, launch_url, mode, game_id, player_id, currency, jurisdiction, expires_at]
      properties:
        launch_id: { type: string, format: uuid }
        launch_url: { type: string, format: uri, description: "bhub's play address; for Kerma Games real-money launches, the game address Kerma's launcher returned (one-time key inside).", examples: ['https://play.bhts.dev/play/0193...'] }
        mode: { type: string, enum: [real, demo] }
        game_id: { type: string, format: uuid }
        player_id: { type: [string, 'null'] }
        currency: { type: string }
        jurisdiction: { type: string }
        expires_at: { type: string, format: date-time }

    Game:
      type: object
      required: [game_id, provider_game_id, name, studio, category, status, status_reason, test_only, certification,
                 rtp, volatility, max_win_multiplier, features, free_spins, min_bet, data_weight_kb, lite, updated_at]
      properties:
        game_id: { type: string, format: uuid }
        provider_game_id: { type: string }
        name: { type: string }
        studio: { type: object, required: [code, name], properties: { code: { type: string }, name: { type: string } } }
        category: { type: string, enum: [slots, crash, instant, table, live, other] }
        status:
          type: string
          enum: [available, hidden, stopped, not_certified, unavailable]
          description: >-
            Launchable only when available (same derivation as the portal's Games screen). `unavailable`
            carries `status_reason`, for example when you have no active connection with the studio
            (`studio_not_connected`) or your contract does not include the game (`not_in_contract`).
        status_reason: { type: [string, 'null'] }
        test_only: { type: boolean, description: Simulator games; real money only in the sandbox. }
        certification:
          type: object
          required: [jurisdiction, status, version, pending_version]
          properties:
            jurisdiction: { type: string }
            status: { type: string, enum: [certified, pending, not_certified, not_required] }
            version: { type: [string, 'null'] }
            pending_version: { type: [string, 'null'] }
        rtp: { type: [string, 'null'], examples: ['96.50'], description: Percent, of the certified configuration. }
        volatility: { type: [string, 'null'], enum: [low, medium, high, very_high, null] }
        max_win_multiplier: { type: [integer, 'null'] }
        features: { type: array, items: { type: string } }
        free_spins: { type: object, required: [supported, levels_url], properties: { supported: { type: boolean }, levels_url: { type: string } } }
        min_bet: { type: object, additionalProperties: { oneOf: [ { $ref: '#/components/schemas/Amount' }, { type: 'null' } ] }, description: Per tenant currency; null when the studio has not said. }
        data_weight_kb: { type: [integer, 'null'], description: First-load download size. }
        lite: { type: [boolean, 'null'], description: A low-data build exists. }
        updated_at: { type: string, format: date-time }
    FreeSpinLevels:
      type: object
      required: [game_id, studio, supported, checked_at, note, currencies]
      properties:
        game_id: { type: string }
        studio: { type: string }
        supported: { type: boolean }
        checked_at: { type: [string, 'null'], format: date-time, description: Last successful sync with the studio. }
        note: { type: string }
        currencies:
          type: array
          items:
            type: object
            required: [currency, missing, source, levels]
            properties:
              currency: { type: string }
              missing: { type: boolean }
              source: { type: [string, 'null'] }
              levels:
                type: array
                items: { type: object, required: [level_key, amount], properties: { level_key: { type: [string, 'null'] }, amount: { $ref: '#/components/schemas/Amount' } } }
    AssetManifest:
      type: object
      required: [cdn_base, generated_at, games]
      properties:
        cdn_base: { type: string, format: uri, examples: ['https://cdn.bhts.dev/games'] }
        generated_at: { type: string, format: date-time }
        games:
          type: array
          items:
            type: object
            required: [game_id, updated_at, assets]
            properties:
              game_id: { type: string }
              updated_at: { type: string, format: date-time }
              assets:
                type: array
                items:
                  type: object
                  required: [kind, format, width, height, url, bytes, sha256, placeholder]
                  properties:
                    kind: { type: string, enum: [thumbnail, banner, icon, background] }
                    format: { type: string, enum: [webp, avif] }
                    width: { type: integer }
                    height: { type: integer }
                    url: { type: string, format: uri }
                    bytes: { type: [integer, 'null'] }
                    sha256: { type: [string, 'null'] }
                    placeholder: { type: boolean, description: Stand-in until the studio's artwork arrives. }

    Round:
      type: object
      required: [round_id, provider_round_id, game_id, provider_game_id, game_name, studio, player_id, currency, stake,
                 win, status, is_free, bonus_ref, platform, opened_at, closed_at, last_adjusted_at, config_version, rtp,
                 jurisdiction, ticket_no, replay_url, round_details, transactions]
      properties:
        round_id: { type: string, format: uuid }
        provider_round_id: { type: string }
        game_id: { type: string }
        provider_game_id: { type: string }
        game_name: { type: string }
        studio: { type: string }
        player_id: { type: string }
        currency: { type: string }
        stake: { $ref: '#/components/schemas/Amount' }
        win: { $ref: '#/components/schemas/Amount' }
        status: { type: string, enum: [open, closed, cancelled] }
        is_free: { type: boolean }
        bonus_ref: { type: [string, 'null'], description: "The studio's free spins reference." }
        platform: { type: string }
        opened_at: { type: string, format: date-time }
        closed_at: { type: [string, 'null'], format: date-time }
        last_adjusted_at: { type: [string, 'null'], format: date-time }
        config_version: { type: [string, 'null'] }
        rtp: { type: [string, 'null'] }
        jurisdiction:
          type: object
          properties: { code: { type: string }, province: { type: [string, 'null'] }, venue: { type: [string, 'null'] } }
        ticket_no: { type: [string, 'null'], description: "WRS ticket number (SANS 1718-4), gap-free per tenant, e.g. ZA-0009843601220. Null for a few seconds after the round commits, and always for sandbox tenants." }
        bet_slip: { $ref: '#/components/schemas/BetSlip' }
        replay_url: { type: [string, 'null'], description: "The studio's replay link for the round when the studio offers one (Kerma Games: per connection); null otherwise." }
        round_details: { description: "The studio's raw round fields, as sent." }
        from_archive: { type: boolean, description: "The round is older than the 90-day hot window and was read from the archive." }
        transactions:
          type: array
          items:
            type: object
            required: [tx_id, kind, debit, credit, operator_tx_id, occurred_at]
            properties:
              tx_id: { type: string }
              kind: { type: string, enum: [bet, win, bet_win, rollback, end_round, promo_credit, adjustment] }
              debit: { $ref: '#/components/schemas/Amount' }
              credit: { $ref: '#/components/schemas/Amount' }
              operator_tx_id: { type: [string, 'null'] }
              occurred_at: { type: string, format: date-time }

    FeedPage:
      type: object
      required: [items, next_cursor, has_more]
      properties:
        next_cursor: { type: string, description: Always present; unchanged on an empty page. }
        has_more: { type: boolean }
    TransactionPage:
      allOf:
        - { $ref: '#/components/schemas/FeedPage' }
        - type: object
          properties:
            items:
              type: array
              items:
                type: object
                properties:
                  tx_id: { type: string }
                  round_id: { type: string }
                  provider_round_id: { type: string }
                  kind: { type: string }
                  player_id: { type: string }
                  game_id: { type: string }
                  provider_game_id: { type: string }
                  currency: { type: string }
                  debit: { $ref: '#/components/schemas/Amount' }
                  credit: { $ref: '#/components/schemas/Amount' }
                  provider_tx_id: { type: string }
                  operator_tx_id: { type: [string, 'null'] }
                  balance_after: { oneOf: [ { $ref: '#/components/schemas/Amount' }, { type: 'null' } ] }
                  occurred_at: { type: string, format: date-time }
                  recorded_at: { type: string, format: date-time }
                  change_seq: { type: integer }
    RoundFeedPage:
      allOf:
        - { $ref: '#/components/schemas/FeedPage' }
        - type: object
          properties:
            items:
              type: array
              items:
                type: object
                properties:
                  round_id: { type: string }
                  provider_round_id: { type: string }
                  player_id: { type: string }
                  game_id: { type: string }
                  provider_game_id: { type: string }
                  currency: { type: string }
                  stake: { $ref: '#/components/schemas/Amount' }
                  win: { $ref: '#/components/schemas/Amount' }
                  status: { type: string, enum: [open, closed, cancelled] }
                  is_free: { type: boolean }
                  bonus_ref: { type: [string, 'null'] }
                  platform: { type: string }
                  opened_at: { type: string, format: date-time }
                  closed_at: { type: [string, 'null'] }
                  last_adjusted_at: { type: [string, 'null'] }
                  ticket_no: { type: [string, 'null'], description: "Set once numbered; the round is re-emitted on the feed when its ticket is issued." }
                  change_seq: { type: integer }
    FailedPage:
      allOf:
        - { $ref: '#/components/schemas/FeedPage' }
        - type: object
          properties:
            items:
              type: array
              items:
                type: object
                properties:
                  tx_id: { type: string }
                  round_id: { type: [string, 'null'] }
                  player_id: { type: [string, 'null'] }
                  operation: { type: string, examples: [win, rollback, end_round, promo_credit] }
                  currency: { type: string }
                  amount: { $ref: '#/components/schemas/Amount' }
                  state: { type: string, enum: [pending, done, manual] }
                  attempts: { type: integer }
                  last_error: { type: [string, 'null'] }
                  next_try_at: { type: [string, 'null'], format: date-time }
                  give_up_at: { type: string, format: date-time }
                  first_failure_at: { type: string, format: date-time }
                  updated_at: { type: string, format: date-time }
                  change_seq: { type: integer }
    DailyTotals:
      type: object
      required: [date, time_zone, jurisdiction, totals, demo]
      properties:
        date: { type: string, format: date }
        time_zone: { type: string, examples: [Africa/Johannesburg] }
        jurisdiction: { type: string }
        totals:
          type: array
          items:
            type: object
            properties:
              date: { type: string, format: date }
              studio: { type: string }
              game_id: { type: string }
              provider_game_id: { type: string }
              game_name: { type: string }
              currency: { type: string }
              rounds: { type: integer }
              turnover: { $ref: '#/components/schemas/Amount' }
              wins: { $ref: '#/components/schemas/Amount' }
              refunds: { $ref: '#/components/schemas/Amount' }
              ggr: { $ref: '#/components/schemas/Amount', description: 'turnover - wins - refunds' }
              free_spins: { type: integer }
              free_spin_wins: { $ref: '#/components/schemas/Amount' }
        demo:
          type: array
          description: "Demo sessions launched on the business day, per game (JSON only, not in the CSV). No money."
          items:
            type: object
            required: [game_id, launches, sessions_reported, players, rounds, free_spins]
            properties:
              game_id: { type: string }
              launches: { type: integer }
              sessions_reported: { type: integer, description: "sessions the studio reported on" }
              players: { type: integer, description: "distinct player_id values you gave" }
              rounds: { type: integer }
              free_spins: { type: integer }

    # ---------------- Webhook payloads (POSTed by bhub) ----------------
    WebhookEnvelope:
      type: object
      required: [event_id, type, created_at, data]
      properties:
        event_id: { type: string, format: uuid, description: Same as X-Bhub-Webhook-Id; deduplicate on it. }
        type: { type: string, enum: [payout.failed, payout.manual, round.force_closed, game.status_changed] }
        created_at: { type: string, format: date-time }
        data:
          oneOf:
            - { $ref: '#/components/schemas/PayoutEvent' }
            - { $ref: '#/components/schemas/RoundForceClosedEvent' }
            - { $ref: '#/components/schemas/GameStatusChangedEvent' }
    PayoutEvent:
      description: 'payout.failed: a credit your wallet has not confirmed (bhub keeps retrying). payout.manual: bhub gave up; settle by hand.'
      type: object
      properties:
        tx_id: { type: string }
        round_id: { type: [string, 'null'] }
        player_id: { type: [string, 'null'] }
        operation: { type: string }
        currency: { type: string }
        amount: { $ref: '#/components/schemas/Amount' }
        attempts: { type: integer }
        last_error: { type: [string, 'null'] }
        first_failure_at: { type: string, format: date-time }
        give_up_at: { type: string, format: date-time }
        next_try_at: { type: string, format: date-time, description: payout.failed only. }
        manual_since: { type: string, format: date-time, description: payout.manual only. }
    RoundForceClosedEvent:
      type: object
      properties:
        round_id: { type: string }
        provider_round_id: { type: string }
        game_id: { type: string }
        player_id: { type: string }
        currency: { type: string }
        stake: { $ref: '#/components/schemas/Amount' }
        win: { $ref: '#/components/schemas/Amount' }
        policy: { type: string, enum: [close_as_is, refund_stake] }
        result: { type: string, enum: [closed, refunded], description: '"refunded" when stakes were returned.' }
        refunded: { $ref: '#/components/schemas/Amount' }
        refund_tx_ids: { type: array, items: { type: string }, description: 'The rollback tx_ids sent to your wallet (empty when nothing was returned).' }
        closed_at: { type: string, format: date-time }
        closed_by: { type: string, examples: ['api-key:opk_...'] }
        reason: { type: string }
    GameStatusChangedEvent:
      type: object
      properties:
        game_id: { type: string }
        provider_game_id: { type: string }
        name: { type: string }
        studio: { type: string }
        previous_status: { type: [string, 'null'] }
        status: { type: string, enum: [available, hidden, stopped, not_certified] }
        status_reason: { type: [string, 'null'] }
        changed_at: { type: string, format: date-time }
