openapi: 3.1.0
info:
  title: Livegest Public API
  version: 1.0.0
  description: API pública REST/JSON da Livegest. Autenticação por Bearer token (chave
    por empresa).
  contact:
    name: Livegest
    url: https://livegest.pt
  license:
    name: Proprietary
servers:
- url: https://qsdehmjotjflpcraaihp.supabase.co/functions/v1/public-api
  description: Produção
security:
- bearerAuth: []
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: API Key (lg_live_...)
  schemas:
    Error:
      type: object
      properties:
        error:
          type: object
          properties:
            code:
              type: string
            message:
              type: string
          required:
          - code
          - message
    Pagination:
      type: object
      properties:
        limit:
          type: integer
        offset:
          type: integer
        total:
          type: integer
    Customer:
      type: object
      properties:
        id:
          type: string
          format: uuid
        name:
          type: string
        tax_id:
          type: string
          nullable: true
        email:
          type: string
          format: email
          nullable: true
        phone:
          type: string
          nullable: true
        address:
          type: string
          nullable: true
        postal_code:
          type: string
          nullable: true
        city:
          type: string
          nullable: true
        country:
          type: string
          nullable: true
        created_at:
          type: string
          format: date-time
      required:
      - id
      - name
    CustomerCreate:
      type: object
      properties:
        name:
          type: string
        tax_id:
          type: string
        email:
          type: string
          format: email
        phone:
          type: string
        address:
          type: string
        postal_code:
          type: string
        city:
          type: string
        country:
          type: string
      required:
      - name
    Product:
      type: object
      properties:
        id:
          type: string
          format: uuid
        name:
          type: string
        sku:
          type: string
          nullable: true
        unit:
          type: string
        unit_price_net:
          type: number
        tax_rate:
          type: number
        stock:
          type: number
          nullable: true
      required:
      - id
      - name
      - unit_price_net
      - tax_rate
    ProductCreate:
      type: object
      properties:
        name:
          type: string
        sku:
          type: string
        unit:
          type: string
          default: un
        unit_price_net:
          type: number
        tax_rate:
          type: number
      required:
      - name
      - unit_price_net
      - tax_rate
    DocumentLine:
      type: object
      properties:
        description:
          type: string
        qty:
          type: number
        unit_price_net:
          type: number
        tax_rate:
          type: number
        discount_pct:
          type: number
          default: 0
        product_id:
          type: string
          format: uuid
          nullable: true
      required:
      - description
      - qty
      - unit_price_net
      - tax_rate
    Document:
      type: object
      properties:
        id:
          type: string
          format: uuid
        doc_type:
          type: string
          enum:
          - FT
          - FS
          - FR
          - NC
          - ND
          - RC
          - GT
          - GR
          - NDV
          - OR
        number:
          type: string
          nullable: true
        atcud:
          type: string
          nullable: true
        hash:
          type: string
          nullable: true
        status:
          type: string
          enum:
          - draft
          - issued
          - cancelled
        issue_date:
          type: string
          format: date
        customer_id:
          type: string
          format: uuid
        total_net:
          type: number
        total_tax:
          type: number
        total_gross:
          type: number
        lines:
          type: array
          items:
            $ref: '#/components/schemas/DocumentLine'
      required:
      - id
      - doc_type
      - status
      - issue_date
      - customer_id
    DocumentCreate:
      type: object
      properties:
        doc_type:
          type: string
          enum:
          - FT
          - FS
          - FR
          - NC
          - ND
          - RC
          - GT
          - GR
          - NDV
          - OR
        series_id:
          type: string
          format: uuid
        issue_date:
          type: string
          format: date
        customer_id:
          type: string
          format: uuid
        issue:
          type: boolean
          default: false
        lines:
          type: array
          items:
            $ref: '#/components/schemas/DocumentLine'
      required:
      - doc_type
      - series_id
      - issue_date
      - customer_id
      - lines
    Payment:
      type: object
      properties:
        id:
          type: string
          format: uuid
        document_id:
          type: string
          format: uuid
        amount:
          type: number
        method_code:
          type: string
        payment_date:
          type: string
          format: date
      required:
      - id
      - document_id
      - amount
      - method_code
      - payment_date
    PaymentCreate:
      type: object
      properties:
        amount:
          type: number
        method_code:
          type: string
        payment_date:
          type: string
          format: date
      required:
      - amount
      - method_code
      - payment_date
  responses:
    Unauthorized:
      description: Chave ausente, inválida ou revogada.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    Forbidden:
      description: Falta scope necessário.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    NotFound:
      description: Recurso não encontrado.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    RateLimited:
      description: Limite excedido (120 req/min).
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    BadRequest:
      description: Dados inválidos.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
  parameters:
    Limit:
      name: limit
      in: query
      schema:
        type: integer
        default: 50
        maximum: 200
    Offset:
      name: offset
      in: query
      schema:
        type: integer
        default: 0
    Search:
      name: search
      in: query
      schema:
        type: string
    Id:
      name: id
      in: path
      required: true
      schema:
        type: string
        format: uuid
paths:
  /v1/me:
    get:
      summary: Informação da chave e empresa
      tags:
      - Meta
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  company:
                    type: object
                  scopes:
                    type: array
                    items:
                      type: string
        '401':
          $ref: '#/components/responses/Unauthorized'
  /v1/customers:
    get:
      summary: Listar clientes
      tags:
      - Clientes
      parameters:
      - $ref: '#/components/parameters/Limit'
      - $ref: '#/components/parameters/Offset'
      - $ref: '#/components/parameters/Search'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/Customer'
                  pagination:
                    $ref: '#/components/schemas/Pagination'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '429':
          $ref: '#/components/responses/RateLimited'
    post:
      summary: Criar cliente
      tags:
      - Clientes
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CustomerCreate'
      responses:
        '201':
          description: Criado
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Customer'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
  /v1/customers/{id}:
    parameters:
    - $ref: '#/components/parameters/Id'
    get:
      summary: Obter cliente
      tags:
      - Clientes
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Customer'
        '404':
          $ref: '#/components/responses/NotFound'
    patch:
      summary: Atualizar cliente
      tags:
      - Clientes
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CustomerCreate'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Customer'
        '404':
          $ref: '#/components/responses/NotFound'
    delete:
      summary: Apagar cliente
      tags:
      - Clientes
      responses:
        '204':
          description: Apagado
        '404':
          $ref: '#/components/responses/NotFound'
  /v1/products:
    get:
      summary: Listar artigos
      tags:
      - Artigos
      parameters:
      - $ref: '#/components/parameters/Limit'
      - $ref: '#/components/parameters/Offset'
      - $ref: '#/components/parameters/Search'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/Product'
                  pagination:
                    $ref: '#/components/schemas/Pagination'
    post:
      summary: Criar artigo
      tags:
      - Artigos
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ProductCreate'
      responses:
        '201':
          description: Criado
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Product'
  /v1/products/{id}:
    parameters:
    - $ref: '#/components/parameters/Id'
    get:
      summary: Obter artigo
      tags:
      - Artigos
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Product'
    patch:
      summary: Atualizar artigo
      tags:
      - Artigos
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ProductCreate'
      responses:
        '200':
          description: OK
    delete:
      summary: Apagar artigo
      tags:
      - Artigos
      responses:
        '204':
          description: Apagado
  /v1/documents:
    get:
      summary: Listar documentos
      tags:
      - Documentos
      parameters:
      - name: doc_type
        in: query
        schema:
          type: string
      - name: status
        in: query
        schema:
          type: string
          enum:
          - draft
          - issued
          - cancelled
      - name: from
        in: query
        schema:
          type: string
          format: date
      - name: to
        in: query
        schema:
          type: string
          format: date
      - $ref: '#/components/parameters/Limit'
      - $ref: '#/components/parameters/Offset'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/Document'
                  pagination:
                    $ref: '#/components/schemas/Pagination'
    post:
      summary: Criar documento
      tags:
      - Documentos
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/DocumentCreate'
      responses:
        '201':
          description: Criado
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Document'
        '400':
          $ref: '#/components/responses/BadRequest'
  /v1/documents/{id}:
    parameters:
    - $ref: '#/components/parameters/Id'
    get:
      summary: Obter documento
      tags:
      - Documentos
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Document'
  /v1/documents/{id}/issue:
    parameters:
    - $ref: '#/components/parameters/Id'
    post:
      summary: Emitir documento
      tags:
      - Documentos
      responses:
        '200':
          description: Emitido
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Document'
  /v1/documents/{id}/cancel:
    parameters:
    - $ref: '#/components/parameters/Id'
    post:
      summary: Anular documento
      tags:
      - Documentos
      responses:
        '200':
          description: Anulado
  /v1/payments:
    get:
      summary: Listar pagamentos
      tags:
      - Pagamentos
      parameters:
      - name: document_id
        in: query
        schema:
          type: string
          format: uuid
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/Payment'
  /v1/documents/{id}/payments:
    parameters:
    - $ref: '#/components/parameters/Id'
    post:
      summary: Registar pagamento
      tags:
      - Pagamentos
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PaymentCreate'
      responses:
        '201':
          description: Criado
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Payment'
tags:
- name: Meta
- name: Clientes
- name: Artigos
- name: Documentos
- name: Pagamentos
