components:
  parameters:
    Nonce:
      description: Unique per state-changing request
      in: header
      name: X-Device-Nonce
      required: true
      schema:
        maxLength: 64
        minLength: 16
        type: string
    Timestamp:
      description: Unix time in ms (±5 minutes)
      in: header
      name: X-Device-Timestamp
      required: true
      schema:
        example: '1764331200000'
        type: string
    VendId:
      in: path
      name: id
      required: true
      schema:
        example: vend_01H...
        type: string
  responses:
    SedraBadRequest:
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/SedraError'
      description: Invalid input
    SedraConflict:
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/SedraError'
      description: Invalid state or replayed request
    SedraForbidden:
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/SedraError'
      description: Authenticated but not allowed
    SedraNotFound:
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/SedraError'
      description: Not found (also returned for items that belong to another business)
    SedraRateLimited:
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/SedraError'
      description: Too many requests
    SedraUnauthorized:
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/SedraError'
      description: Missing, invalid, expired or revoked credentials
  schemas:
    SedraError:
      description: Every Sedra error has this shape.
      example:
        error:
          code: forbidden
          message: Missing scope catalog:write
        ok: false
        status: error
      properties:
        error:
          $ref: '#/components/schemas/SedraErrorBody'
        ok:
          const: false
          type: boolean
        status:
          const: error
          type: string
      required:
        - ok
        - status
        - error
      type: object
    SedraErrorBody:
      properties:
        code:
          enum:
            - unauthenticated
            - forbidden
            - tenant_forbidden
            - tenant_required
            - invalid_input
            - not_found
            - rate_limited
            - idempotency_key_required
            - idempotency_conflict
            - conflict
            - internal
          type: string
        details:
          description: Extra context, e.g. field errors for invalid_input or retry_after_ms for rate_limited
        message:
          type: string
      required:
        - code
        - message
      type: object
    Vend:
      properties:
        amount:
          type: number
        currency_code:
          example: bhd
          type: string
        detail:
          type: string
        instruction:
          example: dispense
          type: string
        order_id:
          type: string
        status:
          enum:
            - created
            - paid
            - dispensed
            - dispense_failed
            - refunded
          type: string
        vend_id:
          type: string
      type: object
  securitySchemes:
    device_token:
      description: Device secret sdr_dev_… issued once at registration
      scheme: bearer
      type: http
info:
  description: |
    **Preview.** For vending machines, kiosks and POS devices (e.g. Foulad ATM).
    Implemented and tested with a software simulator; **no physical hardware has been
    certified yet**. Each device belongs to one business, has its own sales channel and
    stock location, and is issued a secret by the business owner.

    Every request needs `Authorization: Bearer sdr_dev_…` (legacy `fos_dev_…`) and
    `X-Device-Timestamp` (Unix ms, ±5 min). State-changing requests also need a unique
    `X-Device-Nonce` (16–64 chars); reusing one returns 409.

    Safety: the platform records intents and confirmations only. It never actuates
    hardware; dispensing and interlocks stay on the device.
  title: Sedra Device API (Preview)
  version: 0.9.0
openapi: 3.1.0
paths:
  /catalog:
    get:
      description: Published products with stock available at this machine and BHD prices.
      operationId: device_catalog
      parameters:
        - $ref: '#/components/parameters/Timestamp'
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  currency_code:
                    type: string
                  device:
                    example: FATM-001
                    type: string
                  items:
                    items:
                      properties:
                        available:
                          type: integer
                        currency_code:
                          type: string
                        price:
                          type: number
                        sku:
                          type: string
                        title:
                          type: string
                        title_ar:
                          type:
                            - string
                            - 'null'
                        variant:
                          type: string
                        variant_id:
                          type: string
                      type: object
                    type: array
                  synced_at:
                    format: date-time
                    type: string
                type: object
          description: Catalog
        '401':
          $ref: '#/components/responses/SedraUnauthorized'
      summary: Price and availability sync
      tags:
        - Catalog
  /config:
    get:
      description: Display message and maintenance status. Never hardware control.
      operationId: device_config
      parameters:
        - $ref: '#/components/parameters/Timestamp'
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  code:
                    type: string
                  display_message:
                    type:
                      - object
                      - 'null'
                  status:
                    enum:
                      - active
                      - maintenance
                      - disabled
                    type: string
                  timezone:
                    type: string
                type: object
          description: Config
      summary: Remote business configuration
      tags:
        - Catalog
  /reconcile:
    post:
      description: Sets platform stock at this machine to the counted values and reports discrepancies.
      operationId: device_reconcile
      parameters:
        - $ref: '#/components/parameters/Timestamp'
        - $ref: '#/components/parameters/Nonce'
      requestBody:
        content:
          application/json:
            schema:
              properties:
                counts:
                  items:
                    properties:
                      quantity:
                        maximum: 10000
                        minimum: 0
                        type: integer
                      variant_id:
                        type: string
                    required:
                      - variant_id
                      - quantity
                    type: object
                  maxItems: 200
                  minItems: 1
                  type: array
              required:
                - counts
              type: object
        required: true
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  discrepancies:
                    items:
                      properties:
                        counted:
                          type: integer
                        delta:
                          type: integer
                        recorded:
                          type: integer
                        variant_id:
                          type: string
                      type: object
                    type: array
                  reconciled:
                    type: integer
                type: object
          description: Reconciled
      summary: Reconcile physical stock
      tags:
        - Operations
  /telemetry:
    post:
      operationId: device_telemetry
      parameters:
        - $ref: '#/components/parameters/Timestamp'
        - $ref: '#/components/parameters/Nonce'
      requestBody:
        content:
          application/json:
            schema:
              additionalProperties: false
              properties:
                door_open:
                  type: boolean
                error_codes:
                  items:
                    maxLength: 32
                    type: string
                  maxItems: 20
                  type: array
                firmware:
                  maxLength: 40
                  type: string
                maintenance_requested:
                  type: boolean
                power:
                  enum:
                    - mains
                    - battery
                  type: string
                temperature_c:
                  maximum: 90
                  minimum: -40
                  type: number
              type: object
        required: true
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  accepted:
                    type: boolean
                type: object
          description: Accepted
        '409':
          $ref: '#/components/responses/SedraConflict'
      summary: Report health and maintenance status
      tags:
        - Operations
  /vends:
    post:
      description: Validates machine stock and opens a cart with a payment session.
      operationId: device_vend_start
      parameters:
        - $ref: '#/components/parameters/Timestamp'
        - $ref: '#/components/parameters/Nonce'
      requestBody:
        content:
          application/json:
            schema:
              additionalProperties: false
              properties:
                items:
                  items:
                    properties:
                      quantity:
                        maximum: 10
                        minimum: 1
                        type: integer
                      variant_id:
                        type: string
                    required:
                      - variant_id
                      - quantity
                    type: object
                  maxItems: 5
                  minItems: 1
                  type: array
                sandbox_scenario:
                  enum:
                    - success
                    - decline
                  type: string
              required:
                - items
              type: object
        required: true
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Vend'
          description: Vend created
        '404':
          $ref: '#/components/responses/SedraNotFound'
        '409':
          $ref: '#/components/responses/SedraConflict'
      summary: Start a vend
      tags:
        - Vending
  /vends/{id}/dispensed:
    post:
      description: Fulfils from the machine's stock location, then captures the payment.
      operationId: device_vend_dispensed
      parameters:
        - $ref: '#/components/parameters/VendId'
        - $ref: '#/components/parameters/Timestamp'
        - $ref: '#/components/parameters/Nonce'
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Vend'
          description: Dispensed and captured
        '409':
          $ref: '#/components/responses/SedraConflict'
      summary: Confirm dispensing
      tags:
        - Vending
  /vends/{id}/failed:
    post:
      description: >-
        Cancels the order, voiding the uncaptured authorization and releasing the stock reservation. The customer is not
        charged.
      operationId: device_vend_failed
      parameters:
        - $ref: '#/components/parameters/VendId'
        - $ref: '#/components/parameters/Timestamp'
        - $ref: '#/components/parameters/Nonce'
      requestBody:
        content:
          application/json:
            schema:
              properties:
                reason:
                  example: motor_jam
                  maxLength: 200
                  type: string
              type: object
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Vend'
          description: Voided
        '409':
          $ref: '#/components/responses/SedraConflict'
      summary: Report a failed dispense
      tags:
        - Vending
  /vends/{id}/payment:
    post:
      description: The machine's terminal approved; authorizes and creates the order. Payment is **not captured** yet.
      operationId: device_vend_payment
      parameters:
        - $ref: '#/components/parameters/VendId'
        - $ref: '#/components/parameters/Timestamp'
        - $ref: '#/components/parameters/Nonce'
      requestBody:
        content:
          application/json:
            schema:
              properties:
                payment_reference:
                  maxLength: 128
                  type: string
              required:
                - payment_reference
              type: object
        required: true
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Vend'
          description: Paid; dispense now
        '409':
          $ref: '#/components/responses/SedraConflict'
      summary: Confirm payment
      tags:
        - Vending
security:
  - device_token: []
servers:
  - description: Development (demo data, sandbox payments)
    url: https://api.sedra.one/foulad/devices
tags:
  - description: Prices, availability and configuration for this machine.
    name: Catalog
  - description: Select → pay → dispense (or fail) lifecycle.
    name: Vending
  - description: Telemetry and stock reconciliation.
    name: Operations
