components:
  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
    Tokens:
      content:
        application/json:
          schema:
            properties:
              access_token:
                type: string
              expires_in:
                example: 1800
                type: integer
              refresh_token:
                description: Opaque, rotates on every refresh
                type: string
              token_type:
                const: Bearer
                type: string
            required:
              - access_token
              - token_type
              - expires_in
              - refresh_token
            type: object
      description: Session tokens
  schemas:
    Application:
      properties:
        business_name:
          type: string
        created_at:
          format: date-time
          type: string
        decision_note:
          type:
            - string
            - 'null'
        id:
          example: vapp_01H...
          type: string
        kind:
          enum:
            - own_store
            - marketplace_vendor
          type: string
        status:
          enum:
            - pending
            - approved
            - rejected
            - withdrawn
          type: string
      type: object
    Approval:
      properties:
        can_decide:
          type: boolean
        capability:
          type: string
        created_at:
          format: date-time
          type: string
        expires_at:
          format: date-time
          type: string
        id:
          type: string
        preview:
          description: What will change
        reason:
          type:
            - string
            - 'null'
        requested_by:
          type: string
        status:
          type: string
        tenant_id:
          type: string
        vendor_id:
          type:
            - string
            - 'null'
      type: object
    InvokeResult:
      properties:
        approval_id:
          type: string
        capability:
          type: string
        data:
          description: Operation result
        error:
          $ref: '#/components/schemas/SedraErrorBody'
        meta:
          properties:
            audit_id:
              type: string
            notice:
              type: string
            request_id:
              type: string
            untrusted_content:
              type: boolean
          type: object
        ok:
          type: boolean
        status:
          enum:
            - success
            - dry_run
            - pending_approval
            - replayed
            - error
          type: string
        tenant_id:
          type:
            - string
            - 'null'
      required:
        - ok
        - status
        - capability
      type: object
    Me:
      properties:
        application:
          type:
            - object
            - 'null'
        businesses:
          items:
            properties:
              currency_code:
                type: string
              handle:
                type: string
              mode:
                enum:
                  - single
                  - marketplace
                type: string
              name:
                type: string
              primary_color:
                type:
                  - string
                  - 'null'
              role:
                enum:
                  - viewer
                  - operator
                  - manager
                  - owner
                type: string
              status:
                type: string
              tenant_id:
                type: string
              vendor:
                description: Set for marketplace vendor seats
                properties:
                  id:
                    type: string
                  name:
                    type: string
                  name_ar:
                    type:
                      - string
                      - 'null'
                  status:
                    type: string
                type:
                  - object
                  - 'null'
            type: object
          type: array
        experience:
          enum:
            - super_admin
            - merchant
            - applicant
          type: string
        is_super_admin:
          type: boolean
        pending_applications:
          description: Super admins only
          type: integer
        pending_approvals:
          type: integer
        status:
          enum:
            - active
            - application_pending
            - application_rejected
            - no_access
          type: string
        user:
          properties:
            email:
              type:
                - string
                - 'null'
            id:
              type: string
            name:
              type:
                - string
                - 'null'
          type: object
      type: object
    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
  securitySchemes:
    app_session:
      bearerFormat: JWT
      description: Access token from /auth/apple or /auth/google (30 minutes). Refresh with /auth/refresh.
      scheme: bearer
      type: http
info:
  description: |
    **First-party API for Sedra's own apps** (the native Apple app). Third-party
    integrations should use the Agent API. Covers Sign in with Apple/Google, the
    seller waiting list, invitations, approvals and push registration. Business
    operations go through `POST /invoke/{capability}`, which runs the same governed
    operations documented in the Agent API.
  title: Sedra App API
  version: 1.0.0
openapi: 3.1.0
paths:
  /applications:
    get:
      operationId: app_my_applications
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  applications:
                    items:
                      $ref: '#/components/schemas/Application'
                    type: array
                  pending_in_queue:
                    type: integer
                type: object
          description: Applications and queue size
      summary: My applications
      tags:
        - Waiting list
    post:
      description: >-
        `own_store` = your own brand and storefront (a). `marketplace_vendor` = sell inside a marketplace (b); set
        `marketplace`. One pending application per person.
      operationId: app_apply
      requestBody:
        content:
          application/json:
            schema:
              additionalProperties: false
              properties:
                business_name:
                  maxLength: 120
                  minLength: 2
                  type: string
                business_name_ar:
                  maxLength: 120
                  type: string
                category:
                  maxLength: 80
                  type: string
                commercial_registration:
                  description: Bahrain CR number
                  maxLength: 40
                  type: string
                description:
                  maxLength: 2000
                  type: string
                instagram:
                  maxLength: 60
                  type: string
                kind:
                  enum:
                    - own_store
                    - marketplace_vendor
                  type: string
                marketplace:
                  description: Marketplace handle or id (required for marketplace_vendor)
                  maxLength: 80
                  type: string
                phone:
                  pattern: ^\+?[0-9 ]{6,20}$
                  type: string
                website:
                  format: uri
                  maxLength: 200
                  type: string
              required:
                - kind
                - business_name
              type: object
        required: true
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  application:
                    $ref: '#/components/schemas/Application'
                type: object
          description: Application created
        '400':
          $ref: '#/components/responses/SedraBadRequest'
        '409':
          $ref: '#/components/responses/SedraConflict'
      summary: Apply to sell on Sedra
      tags:
        - Waiting list
  /applications/{id}/withdraw:
    post:
      operationId: app_withdraw_application
      parameters:
        - in: path
          name: id
          required: true
          schema:
            type: string
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  id:
                    type: string
                  status:
                    type: string
                type: object
          description: Withdrawn
        '404':
          $ref: '#/components/responses/SedraNotFound'
        '409':
          $ref: '#/components/responses/SedraConflict'
      summary: Withdraw a pending application
      tags:
        - Waiting list
  /approvals:
    get:
      operationId: app_approvals
      parameters:
        - in: query
          name: status
          schema:
            default: pending
            enum:
              - pending
              - approved
              - rejected
              - executed
              - failed
              - expired
            type: string
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  approvals:
                    items:
                      $ref: '#/components/schemas/Approval'
                    type: array
                type: object
          description: Approvals
      summary: Approvals I can see
      tags:
        - Operations
  /approvals/{id}/decide:
    post:
      description: >-
        Managers and owners only; vendor seats only for their own vendor's requests. Approving executes the original
        operation.
      operationId: app_decide_approval
      parameters:
        - in: path
          name: id
          required: true
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              properties:
                decision:
                  enum:
                    - approve
                    - reject
                  type: string
              required:
                - decision
              type: object
        required: true
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  id:
                    type: string
                  result:
                    $ref: '#/components/schemas/InvokeResult'
                  status:
                    enum:
                      - executed
                      - failed
                      - rejected
                    type: string
                type: object
          description: Decision recorded
        '403':
          $ref: '#/components/responses/SedraForbidden'
        '404':
          $ref: '#/components/responses/SedraNotFound'
        '409':
          $ref: '#/components/responses/SedraConflict'
      summary: Approve or reject a sensitive operation
      tags:
        - Operations
  /auth/apple:
    post:
      description: >-
        Send the identity token and the **raw** nonce (Apple receives SHA-256(nonce)). Returns a 30-minute access token
        and a rotating refresh token. Links accounts only by provider-verified email.
      operationId: app_auth_apple
      requestBody:
        content:
          application/json:
            schema:
              properties:
                device_name:
                  maxLength: 80
                  type: string
                full_name:
                  description: Only available on first Apple sign-in
                  maxLength: 120
                  type: string
                identity_token:
                  maxLength: 8000
                  minLength: 20
                  type: string
                nonce:
                  description: Raw nonce
                  maxLength: 200
                  minLength: 16
                  type: string
              required:
                - identity_token
                - nonce
              type: object
        required: true
      responses:
        '200':
          $ref: '#/components/responses/Tokens'
        '401':
          $ref: '#/components/responses/SedraUnauthorized'
        '409':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SedraError'
          description: Sign in with Apple not configured on the server
        '429':
          $ref: '#/components/responses/SedraRateLimited'
      security: []
      summary: Sign in with Apple
      tags:
        - Authentication
  /auth/google:
    post:
      description: Send the Google ID token (GoogleSignIn-iOS `user.idToken.tokenString`).
      operationId: app_auth_google
      requestBody:
        content:
          application/json:
            schema:
              properties:
                device_name:
                  maxLength: 80
                  type: string
                id_token:
                  maxLength: 8000
                  minLength: 20
                  type: string
                nonce:
                  maxLength: 200
                  type: string
              required:
                - id_token
              type: object
        required: true
      responses:
        '200':
          $ref: '#/components/responses/Tokens'
        '401':
          $ref: '#/components/responses/SedraUnauthorized'
        '429':
          $ref: '#/components/responses/SedraRateLimited'
      security: []
      summary: Sign in with Google
      tags:
        - Authentication
  /auth/logout:
    post:
      operationId: app_auth_logout
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  signed_out:
                    type: boolean
                type: object
          description: Session revoked
        '401':
          $ref: '#/components/responses/SedraUnauthorized'
      summary: Sign out this device
      tags:
        - Authentication
  /auth/refresh:
    post:
      description: >-
        Returns new tokens; the old refresh token stops working. Presenting an already-rotated refresh token revokes the
        whole session (theft detection), so sign in again.
      operationId: app_auth_refresh
      requestBody:
        content:
          application/json:
            schema:
              properties:
                refresh_token:
                  type: string
              required:
                - refresh_token
              type: object
        required: true
      responses:
        '200':
          $ref: '#/components/responses/Tokens'
        '401':
          $ref: '#/components/responses/SedraUnauthorized'
      security: []
      summary: Rotate the session
      tags:
        - Authentication
  /capabilities:
    get:
      description: Same catalog as the Agent API, including app-only (human-only) team operations.
      operationId: app_capabilities
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  capabilities:
                    items:
                      type: object
                    type: array
                type: object
          description: Capabilities
      summary: List operations available through invoke
      tags:
        - Operations
  /invitations/redeem:
    post:
      description: >-
        Codes look like `SDR-XXXX-XXXX` (legacy `FOS-…` accepted). Email invitations are accepted automatically on
        sign-in with a verified matching email.
      operationId: app_redeem_invitation
      requestBody:
        content:
          application/json:
            schema:
              properties:
                code:
                  pattern: ^(FOS|SDR)-[A-Z0-9]{4}-[A-Z0-9]{4}$
                  type: string
              required:
                - code
              type: object
        required: true
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  joined:
                    properties:
                      role:
                        type: string
                      tenant_id:
                        type: string
                      vendor_id:
                        type:
                          - string
                          - 'null'
                    type: object
                  me:
                    $ref: '#/components/schemas/Me'
                type: object
          description: Joined
        '404':
          $ref: '#/components/responses/SedraNotFound'
        '409':
          $ref: '#/components/responses/SedraConflict'
      summary: Join a business with an invite code
      tags:
        - Account
  /invoke/{capability}:
    post:
      description: >-
        Same envelope and rules as the Agent API (`POST /foulad/agent/invoke/{capability}`), executed as a human. Writes
        need an `idempotency_key`; sensitive operations return **202** with an `approval_id`.
      operationId: app_invoke
      parameters:
        - in: path
          name: capability
          required: true
          schema:
            example: reports.daily_briefing
            type: string
      requestBody:
        content:
          application/json:
            example:
              input: {}
              tenant: foulad-store
            schema:
              properties:
                dry_run:
                  type: boolean
                idempotency_key:
                  maxLength: 128
                  minLength: 8
                  type: string
                input:
                  type: object
                reason:
                  type: string
                tenant:
                  description: Business handle or id
                  type: string
              type: object
        required: true
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/InvokeResult'
          description: Success, dry run or replay
        '202':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/InvokeResult'
          description: Pending human approval
        '400':
          $ref: '#/components/responses/SedraBadRequest'
        '401':
          $ref: '#/components/responses/SedraUnauthorized'
        '403':
          $ref: '#/components/responses/SedraForbidden'
        '404':
          $ref: '#/components/responses/SedraNotFound'
        '409':
          $ref: '#/components/responses/SedraConflict'
        '429':
          $ref: '#/components/responses/SedraRateLimited'
      summary: Run a business operation as the signed-in person
      tags:
        - Operations
  /marketplaces:
    get:
      operationId: app_marketplaces
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  marketplaces:
                    items:
                      properties:
                        description:
                          type:
                            - object
                            - 'null'
                        handle:
                          type: string
                        name:
                          type: string
                        primary_color:
                          type:
                            - string
                            - 'null'
                        tenant_id:
                          type: string
                      type: object
                    type: array
                type: object
          description: Marketplaces
      summary: Marketplaces accepting vendors
      tags:
        - Waiting list
  /me:
    get:
      description: >-
        `experience` tells the app which home to show: `super_admin`, `merchant` (own businesses or marketplace vendor
        seats) or `applicant` (waiting-list flow).
      operationId: app_me
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Me'
          description: Account
        '401':
          $ref: '#/components/responses/SedraUnauthorized'
      summary: Who am I
      tags:
        - Account
  /push/register:
    post:
      operationId: app_push_register
      requestBody:
        content:
          application/json:
            schema:
              properties:
                app_version:
                  maxLength: 40
                  type: string
                environment:
                  default: production
                  enum:
                    - sandbox
                    - production
                  type: string
                token:
                  pattern: ^[0-9a-fA-F]{32,200}$
                  type: string
              required:
                - token
              type: object
        required: true
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  id:
                    type: string
                  registered:
                    type: boolean
                type: object
          description: Registered
      summary: Register an APNs device token
      tags:
        - Account
  /push/unregister:
    post:
      operationId: app_push_unregister
      requestBody:
        content:
          application/json:
            schema:
              properties:
                token:
                  type: string
              required:
                - token
              type: object
        required: true
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  unregistered:
                    type: boolean
                type: object
          description: Done
      summary: Stop push to a device token
      tags:
        - Account
security:
  - app_session: []
servers:
  - description: Development (demo data, sandbox payments)
    url: https://api.sedra.one/foulad/app
tags:
  - description: Exchange an Apple or Google identity token for a Sedra session.
    name: Authentication
  - description: Who am I, which experience to show, invitations, push.
    name: Account
  - description: Apply to sell on Sedra (own store or marketplace vendor).
    name: Waiting list
  - description: Run governed business operations and decide approvals.
    name: Operations
