{
  "openapi": "3.1.0",
  "info": {
    "title": "UPZI API",
    "version": "1.0.0",
    "description": "Consulte produtos, estoque e pedidos de uma empresa do UPZI. Gere uma chave de administração V2 no painel da empresa e marque os escopos explícitos de cada operação."
  },
  "servers": [
    {
      "url": "https://{host}/v1",
      "description": "API da empresa",
      "variables": {
        "host": {
          "default": "sua-empresa.upzi.com.br",
          "description": "Informe o domínio do painel da empresa, incluindo domínio próprio quando configurado."
        }
      }
    }
  ],
  "security": [{ "empresaBearer": [] }],
  "paths": {
    "/products": {
      "get": {
        "operationId": "listProducts",
        "summary": "Consultar produtos",
        "description": "Requer o escopo products:read.",
        "x-required-scope": "products:read",
        "parameters": [{ "$ref": "#/components/parameters/Limit" }, { "$ref": "#/components/parameters/Offset" }],
        "responses": {
          "200": {
            "description": "Página de produtos",
            "headers": {
              "X-RateLimit-Limit": { "$ref": "#/components/headers/RateLimit" },
              "X-RateLimit-Remaining": { "$ref": "#/components/headers/RateRemaining" },
              "X-RateLimit-Reset": { "$ref": "#/components/headers/RateReset" }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["data", "pagination"],
                  "properties": {
                    "data": { "type": "array", "items": { "$ref": "#/components/schemas/Product" } },
                    "pagination": { "$ref": "#/components/schemas/Pagination" }
                  }
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/InvalidPagination" },
          "401": { "$ref": "#/components/responses/InvalidKey" },
          "403": { "$ref": "#/components/responses/Denied" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "503": { "$ref": "#/components/responses/Unavailable" }
        }
      }
    },
    "/stock": {
      "get": {
        "operationId": "listStock",
        "summary": "Consultar estoque",
        "description": "Requer o escopo products:read. O saldo é o estoque real.",
        "x-required-scope": "products:read",
        "parameters": [{ "$ref": "#/components/parameters/Limit" }, { "$ref": "#/components/parameters/Offset" }],
        "responses": {
          "200": {
            "description": "Página de saldos",
            "headers": {
              "X-RateLimit-Limit": { "$ref": "#/components/headers/RateLimit" },
              "X-RateLimit-Remaining": { "$ref": "#/components/headers/RateRemaining" },
              "X-RateLimit-Reset": { "$ref": "#/components/headers/RateReset" }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["data", "pagination"],
                  "properties": {
                    "data": { "type": "array", "items": { "$ref": "#/components/schemas/Stock" } },
                    "pagination": { "$ref": "#/components/schemas/Pagination" }
                  }
                },
                "example": {
                  "data": [{ "id": "10000000-0000-4000-8000-000000000001", "sku": "SKU-001", "stock": 32 }],
                  "pagination": { "limit": 1, "offset": 0, "next_offset": null }
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/InvalidPagination" },
          "401": { "$ref": "#/components/responses/InvalidKey" },
          "403": { "$ref": "#/components/responses/Denied" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "503": { "$ref": "#/components/responses/Unavailable" }
        }
      }
    },
    "/orders": {
      "get": {
        "operationId": "listOrders",
        "summary": "Consultar pedidos",
        "description": "Requer o escopo orders:read. Retorna apenas dados operacionais do pedido.",
        "x-required-scope": "orders:read",
        "parameters": [{ "$ref": "#/components/parameters/Limit" }, { "$ref": "#/components/parameters/Offset" }],
        "responses": {
          "200": {
            "description": "Página de pedidos",
            "headers": {
              "X-RateLimit-Limit": { "$ref": "#/components/headers/RateLimit" },
              "X-RateLimit-Remaining": { "$ref": "#/components/headers/RateRemaining" },
              "X-RateLimit-Reset": { "$ref": "#/components/headers/RateReset" }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["data", "pagination"],
                  "properties": {
                    "data": { "type": "array", "items": { "$ref": "#/components/schemas/Order" } },
                    "pagination": { "$ref": "#/components/schemas/Pagination" }
                  }
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/InvalidPagination" },
          "401": { "$ref": "#/components/responses/InvalidKey" },
          "403": { "$ref": "#/components/responses/Denied" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "503": { "$ref": "#/components/responses/Unavailable" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "empresaBearer": { "type": "http", "scheme": "bearer", "bearerFormat": "API key" }
    },
    "parameters": {
      "Limit": {
        "name": "limit", "in": "query", "description": "Quantidade máxima de registros nesta página.",
        "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 25 }
      },
      "Offset": {
        "name": "offset", "in": "query", "description": "Posição inicial. Use next_offset da resposta anterior para avançar.",
        "schema": { "type": "integer", "minimum": 0, "maximum": 1000000, "default": 0 }
      }
    },
    "headers": {
      "RateLimit": { "description": "Limite de requisições da chave por janela.", "schema": { "type": "integer" } },
      "RateRemaining": { "description": "Requisições restantes na janela atual.", "schema": { "type": "integer" } },
      "RateReset": { "description": "Fim da janela em segundos Unix.", "schema": { "type": "integer" } }
    },
    "schemas": {
      "Product": {
        "type": "object",
        "required": ["id", "sku", "name", "price", "active"],
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "sku": { "type": ["string", "null"] },
          "name": { "type": "string" },
          "price": { "type": "number", "description": "Preço em reais." },
          "active": { "type": "boolean" }
        }
      },
      "Stock": {
        "type": "object",
        "required": ["id", "sku", "stock"],
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "sku": { "type": ["string", "null"] },
          "stock": { "type": "integer" }
        }
      },
      "Order": {
        "type": "object",
        "required": ["id", "order_number", "product_name", "quantity", "total", "status", "marketplace", "created_at"],
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "order_number": { "type": "string" },
          "product_name": { "type": "string" },
          "quantity": { "type": "integer" },
          "total": { "type": "number", "description": "Valor total em reais." },
          "status": { "type": "string" },
          "marketplace": { "type": ["string", "null"] },
          "created_at": { "type": "string", "format": "date-time" }
        }
      },
      "Pagination": {
        "type": "object",
        "required": ["limit", "offset", "next_offset"],
        "properties": {
          "limit": { "type": "integer", "minimum": 1, "maximum": 100 },
          "offset": { "type": "integer", "minimum": 0, "maximum": 1000000 },
          "next_offset": { "type": ["integer", "null"], "description": "Offset da próxima página. null indica que a leitura acabou." }
        }
      },
      "Error": {
        "type": "object",
        "required": ["error"],
        "properties": { "error": { "type": "string" } }
      }
    },
    "responses": {
      "InvalidPagination": {
        "description": "Parâmetros de paginação inválidos.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" }, "example": { "error": "invalid_pagination" } } }
      },
      "InvalidKey": {
        "description": "Chave ausente, inválida, vencida, revogada ou sem vínculo ativo.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" }, "example": { "error": "invalid_api_key" } } }
      },
      "Denied": {
        "description": "A chave precisa do escopo explícito do recurso.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" }, "example": { "error": "insufficient_scope" } } }
      },
      "RateLimited": {
        "description": "Limite de requisições atingido.",
        "headers": { "Retry-After": { "description": "Segundos até a próxima tentativa.", "schema": { "type": "integer" } } },
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" }, "example": { "error": "rate_limited" } } }
      },
      "Unavailable": {
        "description": "Serviço temporariamente indisponível.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" }, "example": { "error": "unavailable" } } }
      }
    }
  }
}
