{
  "openapi": "3.0.3",
  "info": {
    "title": "Notreve Contábil API",
    "version": "1.0.0",
    "description": "API de leitura de empresas e notas fiscais. Autenticação por chave da organização."
  },
  "servers": [{ "url": "https://api.contabil.notreve.com.br" }],
  "security": [{ "bearerAuth": [] }],
  "components": {
    "securitySchemes": {
      "bearerAuth": { "type": "http", "scheme": "bearer", "bearerFormat": "chave_..." }
    },
    "schemas": {
      "Empresa": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "cnpj": { "type": "string" },
          "razao_social": { "type": "string" },
          "nome_fantasia": { "type": "string" },
          "uf": { "type": "string" },
          "cidade": { "type": "string" },
          "ativo": { "type": "boolean" },
          "modelos": { "type": "array", "items": { "type": "string" } },
          "retencao_dias": { "type": "integer" }
        }
      },
      "Documento": {
        "type": "object",
        "properties": {
          "id": { "type": "integer", "format": "int64" },
          "chave": { "type": "string" },
          "empresa_id": { "type": "string", "format": "uuid" },
          "modelo": { "type": "string", "enum": ["nfs-e", "nfe", "nfce", "cte", "mdfe", "nfcom"] },
          "tipo": { "type": "string" },
          "competencia": { "type": "string", "example": "2026-10" },
          "cancelada": { "type": "boolean" },
          "papel": { "type": "string" },
          "expurgada": { "type": "boolean" },
          "criado_em": { "type": "string", "format": "date-time" },
          "retencao_dias": { "type": "integer" },
          "expira_em": { "type": "string", "format": "date-time" }
        }
      },
      "Paginacao": {
        "type": "object",
        "properties": {
          "proximo_cursor": {},
          "tem_mais": { "type": "boolean" }
        }
      },
      "Erro": {
        "type": "object",
        "properties": {
          "erro": {
            "type": "object",
            "properties": {
              "codigo": { "type": "string" },
              "mensagem": { "type": "string" },
              "detalhes": {}
            }
          },
          "requisicao_id": { "type": "string" }
        }
      }
    }
  },
  "paths": {
    "/v1/empresas": {
      "get": {
        "summary": "Listar empresas",
        "parameters": [
          { "name": "limite", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 200, "default": 50 } },
          { "name": "cursor", "in": "query", "schema": { "type": "string" }, "description": "Último CNPJ retornado" }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": { "application/json": { "schema": {
              "type": "object",
              "properties": {
                "dados": { "type": "array", "items": { "$ref": "#/components/schemas/Empresa" } },
                "paginacao": { "$ref": "#/components/schemas/Paginacao" },
                "requisicao_id": { "type": "string" }
              }
            } } }
          },
          "401": { "description": "Chave inválida", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Erro" } } } }
        }
      }
    },
    "/v1/empresas/{id}": {
      "get": {
        "summary": "Consultar empresa",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }],
        "responses": {
          "200": { "description": "OK", "content": { "application/json": { "schema": {
            "type": "object",
            "properties": {
              "dados": { "$ref": "#/components/schemas/Empresa" },
              "requisicao_id": { "type": "string" }
            }
          } } } },
          "404": { "description": "Não encontrada", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Erro" } } } }
        }
      }
    },
    "/v1/documentos": {
      "get": {
        "summary": "Listar documentos",
        "parameters": [
          { "name": "empresa_id", "in": "query", "schema": { "type": "string", "format": "uuid" } },
          { "name": "modelo", "in": "query", "schema": { "type": "string" } },
          { "name": "competencia_de", "in": "query", "schema": { "type": "string", "example": "2026-10" } },
          { "name": "competencia_ate", "in": "query", "schema": { "type": "string", "example": "2026-10" } },
          { "name": "limite", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 200, "default": 50 } },
          { "name": "cursor", "in": "query", "schema": { "type": "integer", "format": "int64" }, "description": "Último id retornado" }
        ],
        "responses": {
          "200": { "description": "OK", "content": { "application/json": { "schema": {
            "type": "object",
            "properties": {
              "dados": { "type": "array", "items": { "$ref": "#/components/schemas/Documento" } },
              "paginacao": { "$ref": "#/components/schemas/Paginacao" },
              "requisicao_id": { "type": "string" }
            }
          } } } }
        }
      }
    },
    "/v1/documentos/{id}": {
      "get": {
        "summary": "Consultar documento",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string" }, "description": "id numérico ou chave de 44 dígitos" }],
        "responses": {
          "200": { "description": "OK", "content": { "application/json": { "schema": {
            "type": "object",
            "properties": {
              "dados": { "$ref": "#/components/schemas/Documento" },
              "requisicao_id": { "type": "string" }
            }
          } } } },
          "404": { "description": "Não encontrado", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Erro" } } } }
        }
      }
    },
    "/v1/documentos/{id}/xml": {
      "get": {
        "summary": "Baixar XML",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }],
        "responses": {
          "200": { "description": "XML", "content": { "application/xml": { "schema": { "type": "string", "format": "binary" } } } },
          "410": { "description": "Conteúdo expurgado" }
        }
      }
    },
    "/v1/documentos/{id}/pdf": {
      "get": {
        "summary": "Baixar PDF",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }],
        "responses": {
          "200": { "description": "PDF", "content": { "application/pdf": { "schema": { "type": "string", "format": "binary" } } } },
          "410": { "description": "Conteúdo expurgado" },
          "422": { "description": "PDF indisponível para o modelo" }
        }
      }
    },
    "/v1/documentos/{id}/link": {
      "post": {
        "summary": "Gerar link temporário",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "tipo": { "type": "string", "enum": ["xml", "pdf"], "default": "pdf" },
                  "validade_minutos": { "type": "integer", "minimum": 1, "maximum": 15, "default": 15 }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": { "application/json": { "schema": {
              "type": "object",
              "properties": {
                "dados": {
                  "type": "object",
                  "properties": {
                    "url": { "type": "string" },
                    "expira_em": { "type": "string", "format": "date-time" },
                    "tipo": { "type": "string" }
                  }
                },
                "requisicao_id": { "type": "string" }
              }
            } } }
          },
          "401": { "description": "Chave inválida" },
          "410": { "description": "Conteúdo expurgado" }
        }
      }
    },
    "/arquivos/{token}": {
      "get": {
        "summary": "Baixar por link temporário",
        "security": [],
        "parameters": [{ "name": "token", "in": "path", "required": true, "schema": { "type": "string" } }],
        "responses": {
          "200": {
            "description": "Arquivo",
            "content": {
              "application/pdf": { "schema": { "type": "string", "format": "binary" } },
              "application/xml": { "schema": { "type": "string", "format": "binary" } }
            }
          },
          "401": { "description": "Link inválido" },
          "410": { "description": "Link expirado ou conteúdo expurgado" }
        }
      }
    }
  }
}
