openapi: 3.1.0
info:
  title: ACESAR Sandbox API
  version: 1.0.0
  description: |
    API REST de sandbox para pagamentos (PIX, boleto, cartão).
    Ambiente de testes — sem dinheiro real.
  contact:
    url: https://acesar.pages.dev/docs/como-usar

servers:
  - url: https://acesar.pages.dev
    description: Produção (Cloudflare Pages)
  - url: http://localhost:8787
    description: Local (wrangler pages dev)

security:
  - bearerAuth: []

tags:
  - name: payments
    description: Contrato oficial ACESAR v1
  - name: sandbox
    description: Utilitários de sandbox e painel

paths:
  /api/v1/payments:
    post:
      tags: [payments]
      summary: Criar pagamento
      operationId: createPayment
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreatePaymentRequest'
            example:
              amount: 150
              method: pix
              description: Pedido #001
              due_date: '2026-06-01'
              external_reference: pedido_001
              payer:
                email: cliente@teste.com
                name: Cliente Teste
      responses:
        '201':
          description: Pagamento criado
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Payment'
        '400':
          $ref: '#/components/responses/InvalidRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
    get:
      tags: [payments]
      summary: Listar pagamentos
      operationId: listPayments
      responses:
        '200':
          description: Lista paginada
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/PaymentSummary'
                  total:
                    type: integer

  /api/v1/payments/{id}:
    get:
      tags: [payments]
      summary: Consultar pagamento
      operationId: getPayment
      parameters:
        - $ref: '#/components/parameters/PaymentId'
      responses:
        '200':
          description: Pagamento
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Payment'
        '404':
          $ref: '#/components/responses/NotFound'

  /api/v1/payments/{id}/approve:
    post:
      tags: [payments]
      summary: Simular aprovação (sandbox / dev)
      description: |
        Marca o pagamento como `approved` e dispara webhook `payment.approved`.
        Use em desenvolvimento para testar fluxo pós-pagamento.
      operationId: approvePayment
      parameters:
        - $ref: '#/components/parameters/PaymentId'
      responses:
        '200':
          description: Pagamento aprovado
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Payment'
        '404':
          $ref: '#/components/responses/NotFound'

  /api/v1/payments/{id}/refund:
    post:
      tags: [payments]
      summary: Reembolsar pagamento
      operationId: refundPayment
      parameters:
        - $ref: '#/components/parameters/PaymentId'
      responses:
        '200':
          description: Pagamento reembolsado
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Payment'

  /acesar/health:
    get:
      tags: [sandbox]
      summary: Health check
      security: []
      responses:
        '200':
          description: API online
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                  version:
                    type: string

  /acesar/webhooks:
    post:
      tags: [sandbox]
      summary: Cadastrar webhook
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [url]
              properties:
                url:
                  type: string
                  format: uri
                secret:
                  type: string
      responses:
        '201':
          description: Webhook criado

  /acesar/payments/{id}/simulate/{event}:
    post:
      tags: [sandbox]
      summary: Simular evento (legado)
      parameters:
        - $ref: '#/components/parameters/PaymentId'
        - name: event
          in: path
          required: true
          schema:
            type: string
            enum: [approved, overdue, refunded]
      responses:
        '200':
          description: Evento simulado

components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: Chave de API (`acesar_test_default_key` ou chave do painel)

  parameters:
    PaymentId:
      name: id
      in: path
      required: true
      schema:
        type: string
        format: uuid

  schemas:
    Payer:
      type: object
      properties:
        email:
          type: string
          format: email
        name:
          type: string
          nullable: true

    Pix:
      type: object
      description: Schema PIX unificado ACESAR
      required: [qr_code, copy_paste, qr_code_base64, expires_at]
      properties:
        qr_code:
          type: string
          description: EMV / copia e cola
        copy_paste:
          type: string
          description: Alias de qr_code (mesmo valor)
        qr_code_base64:
          type: string
          description: Imagem QR em base64 (placeholder no sandbox)
        expires_at:
          type: string
          format: date-time

    CreatePaymentRequest:
      type: object
      required: [amount, method]
      properties:
        amount:
          type: number
          minimum: 0.01
        method:
          type: string
          enum: [pix, boleto, credit_card]
        description:
          type: string
        due_date:
          type: string
          format: date
        external_reference:
          type: string
        payer:
          $ref: '#/components/schemas/Payer'

    Payment:
      type: object
      properties:
        id:
          type: string
          format: uuid
        object:
          type: string
          example: payment
        status:
          type: string
          enum: [pending, approved, overdue, refunded]
        amount:
          type: number
        currency:
          type: string
          example: BRL
        method:
          type: string
        description:
          type: string
        external_reference:
          type: string
          nullable: true
        due_date:
          type: string
          nullable: true
        payer:
          $ref: '#/components/schemas/Payer'
        pix:
          $ref: '#/components/schemas/Pix'
          nullable: true
        boleto:
          type: object
          nullable: true
        created_at:
          type: string
          format: date-time
        updated_at:
          type: string
          format: date-time
        paid_at:
          type: string
          format: date-time
          nullable: true

    PaymentSummary:
      type: object
      properties:
        id:
          type: string
        object:
          type: string
        status:
          type: string
        amount:
          type: number
        method:
          type: string
        created_at:
          type: string

    Error:
      type: object
      properties:
        error:
          type: string
        message:
          type: string

  responses:
    Unauthorized:
      description: Chave ausente ou inválida
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    InvalidRequest:
      description: Corpo inválido
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    NotFound:
      description: Recurso não encontrado
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
