{
  "openapi": "3.0.3",
  "info": {
    "title": "Omniflow API",
    "description": "API externa do Omniflow para integração via automações. Use o **access_token** obtido em Configurações > API como Bearer token.\n\n## Autenticação\n\nTodas as requisições exigem o header:\n```\nAuthorization: Bearer <seu_access_token>\n```\n\nO access_token é vinculado a um canal (WhatsApp ou Instagram) e só tem acesso aos recursos daquele canal.\n\n## Escopo e segurança (regra imutável)\n\n**O access_token NUNCA deve acessar recursos fora do escopo da API externa.** O token só acessa dados do tenant e canal ao qual foi vinculado.\n\n## Whitelabel\n\nA API e sua base URL respeitam a lógica de Whitelabel. Em instalações com domínio customizado, use a URL pública configurada para o tenant.",
    "version": "1.0.0"
  },
  "servers": [
    { "url": "https://api.omniflow.chat", "description": "Produção" },
    { "url": "http://localhost:3001", "description": "Desenvolvimento local" }
  ],
  "security": [{ "bearerAuth": [] }],
  "paths": {
    "/api/v1/contacts": {
      "get": {
        "tags": ["Plataforma"],
        "summary": "Listar contatos",
        "description": "Lista contatos do tenant associados ao canal da API Key.",
        "parameters": [
          { "name": "page", "in": "query", "schema": { "type": "integer", "default": 1 } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "default": 50 } }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "object",
                      "properties": {
                        "data": { "type": "array", "items": { "$ref": "#/components/schemas/Contact" } },
                        "total": { "type": "integer" },
                        "page": { "type": "integer" },
                        "limit": { "type": "integer" }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/conversations": {
      "get": {
        "tags": ["Plataforma"],
        "summary": "Listar conversas",
        "description": "Lista conversas (sessões) do canal.",
        "parameters": [
          { "name": "page", "in": "query", "schema": { "type": "integer", "default": 1 } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "default": 50 } }
        ],
        "responses": {
          "200": { "description": "OK" }
        }
      }
    },
    "/api/v1/conversations/{id}/messages": {
      "get": {
        "tags": ["Plataforma"],
        "summary": "Listar mensagens",
        "description": "Lista mensagens de uma conversa.",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }],
        "responses": {
          "200": { "description": "OK" },
          "404": { "description": "Conversa não encontrada" }
        }
      }
    },
    "/api/v1/send/text": {
      "post": {
        "tags": ["Envio 1:1"],
        "summary": "Enviar texto",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["to", "text"],
                "properties": {
                  "to": { "type": "string", "description": "Número (WhatsApp) ou IGSID (Instagram)" },
                  "text": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "OK" },
          "400": { "description": "Parâmetros inválidos" }
        }
      }
    },
    "/api/v1/send/image": {
      "post": {
        "tags": ["Envio 1:1"],
        "summary": "Enviar imagem",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["to", "image"],
                "properties": {
                  "to": { "type": "string" },
                  "image": { "type": "string", "description": "Base64 ou URL" },
                  "caption": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": { "200": { "description": "OK" } }
      }
    },
    "/api/v1/send/video": {
      "post": {
        "tags": ["Envio 1:1"],
        "summary": "Enviar vídeo",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["to", "video"],
                "properties": {
                  "to": { "type": "string" },
                  "video": { "type": "string" },
                  "caption": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": { "200": { "description": "OK" } }
      }
    },
    "/api/v1/send/audio": {
      "post": {
        "tags": ["Envio 1:1"],
        "summary": "Enviar áudio",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["to", "audio"],
                "properties": {
                  "to": { "type": "string" },
                  "audio": { "type": "string" },
                  "asVoice": { "type": "boolean", "default": false }
                }
              }
            }
          }
        },
        "responses": { "200": { "description": "OK" } }
      }
    },
    "/api/v1/send/document": {
      "post": {
        "tags": ["Envio 1:1"],
        "summary": "Enviar documento",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["to", "document"],
                "properties": {
                  "to": { "type": "string" },
                  "document": { "type": "string" },
                  "fileName": { "type": "string" },
                  "caption": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": { "200": { "description": "OK" } }
      }
    },
    "/api/v1/groups": {
      "get": {
        "tags": ["Grupos (WhatsApp)"],
        "summary": "Listar grupos",
        "description": "Lista grupos do WhatsApp (apenas canais WhatsApp).",
        "responses": { "200": { "description": "OK" } }
      }
    },
    "/api/v1/groups/{groupId}/send/text": {
      "post": {
        "tags": ["Grupos (WhatsApp)"],
        "summary": "Enviar texto para grupo",
        "parameters": [{ "name": "groupId", "in": "path", "required": true, "schema": { "type": "string" } }],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["text"],
                "properties": { "text": { "type": "string" } }
              }
            }
          }
        },
        "responses": { "200": { "description": "OK" } }
      }
    },
    "/api/v1/groups/{groupId}/send/image": {
      "post": {
        "tags": ["Grupos (WhatsApp)"],
        "summary": "Enviar imagem para grupo",
        "parameters": [{ "name": "groupId", "in": "path", "required": true, "schema": { "type": "string" } }],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["image"],
                "properties": {
                  "image": { "type": "string" },
                  "caption": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": { "200": { "description": "OK" } }
      }
    },
    "/api/v1/groups/{groupId}/send/file": {
      "post": {
        "tags": ["Grupos (WhatsApp)"],
        "summary": "Enviar arquivo para grupo",
        "parameters": [{ "name": "groupId", "in": "path", "required": true, "schema": { "type": "string" } }],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["file"],
                "properties": {
                  "file": { "type": "string" },
                  "fileName": { "type": "string" },
                  "caption": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": { "200": { "description": "OK" } }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "custom",
        "description": "Access token gerado em Configurações > API"
      }
    },
    "schemas": {
      "Contact": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "name": { "type": "string" },
          "phone": { "type": "string", "nullable": true },
          "externalId": { "type": "string", "nullable": true },
          "email": { "type": "string", "nullable": true }
        }
      }
    }
  }
}
