{
  "openapi": "3.0.3",
  "info": {
    "title": "Plugue Email API",
    "version": "1.0.0",
    "description": "API REST do Plugue Email: envio de e-mail transacional, métricas de entregabilidade e validação de endereços. Autenticação por API key (Authorization: Bearer ou x-api-key). Respostas sempre trazem o campo booleano `ok`.",
    "contact": { "name": "Plugue Email", "url": "https://pluguemail.com.br" }
  },
  "servers": [{ "url": "https://pluguemail.com.br/v1" }],
  "security": [{ "bearerAuth": [] }, { "apiKeyHeader": [] }],
  "tags": [
    { "name": "Envio", "description": "Enviar e consultar mensagens" },
    { "name": "Métricas", "description": "Métricas de entregabilidade" },
    { "name": "Validação", "description": "Raio-X de e-mail (valida sem enviar)" }
  ],
  "paths": {
    "/send": {
      "post": {
        "tags": ["Envio"],
        "summary": "Envia um e-mail",
        "description": "Envia um e-mail. Requer escopo `enviar`. Suporta template+variáveis, CC/CCO, anexos, imagens inline (CID), headers, tracking, agendamento (send_at) e idempotência (header Idempotency-Key).",
        "parameters": [
          { "name": "Idempotency-Key", "in": "header", "required": false, "schema": { "type": "string" }, "description": "Repetir a mesma chave devolve a resposta original sem reenviar." }
        ],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/SendRequest" } } }
        },
        "responses": {
          "200": { "description": "Enviado ou agendado", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/SendResponse" } } } },
          "400": { "$ref": "#/components/responses/Erro" },
          "401": { "$ref": "#/components/responses/Erro" },
          "403": { "$ref": "#/components/responses/Erro" },
          "409": { "$ref": "#/components/responses/Erro" },
          "429": { "$ref": "#/components/responses/Erro" }
        }
      }
    },
    "/send/batch": {
      "post": {
        "tags": ["Envio"],
        "summary": "Envia várias mensagens (até 50)",
        "description": "Requer escopo `enviar`. Cada item tem o mesmo formato de /send.",
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": {
            "type": "object", "required": ["messages"],
            "properties": { "messages": { "type": "array", "maxItems": 50, "items": { "$ref": "#/components/schemas/SendRequest" } } }
          } } }
        },
        "responses": {
          "200": { "description": "Resultado do lote", "content": { "application/json": { "schema": {
            "type": "object",
            "properties": {
              "ok": { "type": "boolean" }, "total": { "type": "integer" }, "enviados": { "type": "integer" }, "falhas": { "type": "integer" },
              "resultados": { "type": "array", "items": { "type": "object" } }
            }
          } } } },
          "400": { "$ref": "#/components/responses/Erro" }
        }
      }
    },
    "/messages/{id}": {
      "get": {
        "tags": ["Envio"],
        "summary": "Status de uma mensagem",
        "description": "Requer escopo `ler`.",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }],
        "responses": {
          "200": { "description": "Mensagem", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Message" } } } },
          "404": { "$ref": "#/components/responses/Erro" }
        }
      },
      "delete": {
        "tags": ["Envio"],
        "summary": "Cancela uma mensagem agendada",
        "description": "Requer escopo `enviar`. Só cancela mensagens com status `pendente`.",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }],
        "responses": {
          "200": { "description": "Cancelada", "content": { "application/json": { "schema": {
            "type": "object", "properties": { "ok": { "type": "boolean" }, "id": { "type": "string" }, "status": { "type": "string", "example": "cancelado" } }
          } } } },
          "404": { "$ref": "#/components/responses/Erro" },
          "409": { "$ref": "#/components/responses/Erro" }
        }
      }
    },
    "/metrics": {
      "get": {
        "tags": ["Métricas"],
        "summary": "Métricas de entregabilidade",
        "description": "Requer escopo `ler`.",
        "parameters": [
          { "name": "dias", "in": "query", "schema": { "type": "integer", "default": 30, "minimum": 1, "maximum": 365 } },
          { "name": "incluir", "in": "query", "schema": { "type": "string", "example": "dominios,provedores,serie" }, "description": "Blocos extras separados por vírgula." }
        ],
        "responses": {
          "200": { "description": "Métricas", "content": { "application/json": { "schema": {
            "type": "object",
            "properties": {
              "ok": { "type": "boolean" }, "dias": { "type": "integer" },
              "geral": { "type": "object" }, "comparacao": { "type": "object" },
              "dominios": { "type": "array", "items": { "type": "object" } },
              "provedores": { "type": "array", "items": { "type": "object" } },
              "serie": { "type": "array", "items": { "type": "object" } }
            }
          } } } }
        }
      }
    },
    "/validate": {
      "post": {
        "tags": ["Validação"],
        "summary": "Valida um e-mail (sem enviar)",
        "requestBody": { "required": true, "content": { "application/json": { "schema": {
          "type": "object", "required": ["email"], "properties": { "email": { "type": "string", "example": "fulano@gmail.com" } }
        } } } },
        "responses": {
          "200": { "description": "Resultado", "content": { "application/json": { "schema": {
            "type": "object", "properties": { "ok": { "type": "boolean" }, "resultado": { "$ref": "#/components/schemas/Validacao" } }
          } } } },
          "400": { "$ref": "#/components/responses/Erro" }
        }
      }
    },
    "/validate/batch": {
      "post": {
        "tags": ["Validação"],
        "summary": "Valida até 200 e-mails",
        "requestBody": { "required": true, "content": { "application/json": { "schema": {
          "type": "object", "required": ["emails"],
          "properties": { "emails": { "oneOf": [ { "type": "array", "items": { "type": "string" } }, { "type": "string" } ], "description": "Lista ou texto (um por linha / vírgula)." } }
        } } } },
        "responses": {
          "200": { "description": "Resultados", "content": { "application/json": { "schema": {
            "type": "object", "properties": {
              "ok": { "type": "boolean" }, "total": { "type": "integer" },
              "resultados": { "type": "array", "items": { "$ref": "#/components/schemas/Validacao" } }
            }
          } } } },
          "400": { "$ref": "#/components/responses/Erro" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": { "type": "http", "scheme": "bearer", "description": "Authorization: Bearer SUA_API_KEY" },
      "apiKeyHeader": { "type": "apiKey", "in": "header", "name": "x-api-key" }
    },
    "responses": {
      "Erro": { "description": "Erro", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Erro" } } } }
    },
    "schemas": {
      "SendRequest": {
        "type": "object",
        "required": ["from", "to"],
        "properties": {
          "from": { "type": "string", "description": "Remetente (domínio verificado na conta).", "example": "Equipe <contato@seudominio.com>" },
          "to": { "oneOf": [ { "type": "string" }, { "type": "array", "items": { "type": "string" } } ] },
          "cc": { "oneOf": [ { "type": "string" }, { "type": "array", "items": { "type": "string" } } ] },
          "bcc": { "oneOf": [ { "type": "string" }, { "type": "array", "items": { "type": "string" } } ] },
          "subject": { "type": "string" },
          "html": { "type": "string" },
          "text": { "type": "string" },
          "template_id": { "type": "integer" },
          "variables": { "type": "object", "additionalProperties": true },
          "reply_to": { "type": "string" },
          "attachments": { "type": "array", "items": { "$ref": "#/components/schemas/Attachment" } },
          "headers": { "type": "object", "additionalProperties": { "type": "string" } },
          "tags": { "type": "array", "items": { "type": "object" } },
          "category": { "type": "string" },
          "metadata": { "type": "object", "additionalProperties": true },
          "track": { "type": "boolean" },
          "unsubscribe": { "type": "boolean" },
          "send_at": { "type": "string", "format": "date-time", "example": "2026-10-10T14:00:00Z" },
          "sandbox": { "type": "boolean" },
          "idempotency_key": { "type": "string" }
        }
      },
      "Attachment": {
        "type": "object",
        "required": ["filename", "content"],
        "properties": {
          "filename": { "type": "string" },
          "content": { "type": "string", "description": "Conteúdo em base64." },
          "content_type": { "type": "string" },
          "cid": { "type": "string", "description": "Content-ID para imagem inline (src=\"cid:...\")." }
        }
      },
      "SendResponse": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean" },
          "id": { "type": "string" },
          "messageId": { "type": "string" },
          "status": { "type": "string", "example": "agendado" },
          "agendado_para": { "type": "string", "format": "date-time", "nullable": true }
        }
      },
      "Message": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean" },
          "id": { "type": "string" },
          "to": { "type": "string" },
          "subject": { "type": "string" },
          "status": { "type": "string", "enum": ["pendente", "enviando", "enviado", "falha", "cancelado"] },
          "agendado_para": { "type": "string", "format": "date-time", "nullable": true },
          "messageId": { "type": "string", "nullable": true },
          "erro": { "type": "string", "nullable": true },
          "criado_em": { "type": "string", "format": "date-time" }
        }
      },
      "Validacao": {
        "type": "object",
        "properties": {
          "normalizado": { "type": "string" },
          "dominio": { "type": "string", "nullable": true },
          "valido": { "type": "boolean" },
          "score": { "type": "integer", "minimum": 0, "maximum": 100 },
          "band": { "type": "string", "enum": ["valido", "arriscado", "invalido"] },
          "mx": { "type": "boolean" },
          "descartavel": { "type": "boolean" },
          "role_based": { "type": "boolean" },
          "gratuito": { "type": "boolean" },
          "sugestao": { "type": "string", "nullable": true },
          "motivos": { "type": "array", "items": { "type": "string" } }
        }
      },
      "Erro": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean", "example": false },
          "codigo": { "type": "string", "example": "dominio_nao_verificado" },
          "erro": { "type": "string" }
        }
      }
    }
  }
}
