{
  "name": "Sal\u00e3o Inteligente",
  "active": true,
  "nodes": [
    {
      "parameters": {
        "path": "api/whatsapp",
        "options": {}
      },
      "id": "Webhook_Whatsapp",
      "name": "Webhook WhatsApp",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        240,
        300
      ],
      "webhookDescription": "Recebe mensagens da Evolution API"
    },
    {
      "parameters": {
        "resource": "completion",
        "model": "gpt-4o",
        "text": "Voc\u00ea \u00e9 o assistente virtual do Sal\u00e3o Inteligente. Atenda clientes, agende servi\u00e7os e envie lembretes.",
        "additionalFields": {}
      },
      "id": "OpenAI_LLM",
      "name": "GPT-4o Atendente",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 1,
      "position": [
        600,
        300
      ]
    },
    {
      "parameters": {
        "url": "{{$env.EVOLUTION_API_URL}}/message/sendText",
        "method": "POST",
        "authentication": "none",
        "jsonParameters": true,
        "options": {},
        "bodyParametersJson": "{\"number\": \"{{$json[\\\"from\\\"]}}\", \"text\": \"{{$json[\\\"reply\\\"]}}\"}",
        "headerParametersJson": "{\"Authorization\": \"Bearer {{$env.EVOLUTION_API_TOKEN}}\",\"Content-Type\":\"application/json\"}"
      },
      "id": "Evolution_Send",
      "name": "Enviar WhatsApp",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        960,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "create table if not exists logs (id uuid primary key default gen_random_uuid(), tipo text, origem text, referencia text, conteudo jsonb, criado_em timestamptz default now());"
      },
      "id": "Supabase_Setup",
      "name": "Supabase Setup",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        240,
        600
      ]
    },
    {
      "parameters": {
        "cronExpression": "*/5 * * * *"
      },
      "id": "Cron_Lembrete",
      "name": "Lembretes Autom\u00e1ticos",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        240,
        900
      ]
    },
    {
      "parameters": {
        "path": "admin",
        "options": {}
      },
      "id": "Webhook_Admin",
      "name": "Painel Admin",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        240,
        1200
      ],
      "webhookDescription": "Painel protegido por ADMIN_TOKEN"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "create table if not exists clientes (id uuid primary key default gen_random_uuid(), nome text not null, telefone text unique, criado_em timestamptz default now()); create table if not exists profissionais (id uuid primary key default gen_random_uuid(), nome text not null, comissao_servico numeric default 0.4, comissao_produto numeric default 0.15); create table if not exists servicos (id uuid primary key default gen_random_uuid(), nome text not null, preco numeric not null, duracao_minutos integer not null); create table if not exists produtos (id uuid primary key default gen_random_uuid(), nome text not null, preco numeric not null, estoque integer default 0); create table if not exists agendamentos (id uuid primary key default gen_random_uuid(), cliente_id uuid references clientes(id), profissional_id uuid references profissionais(id), servico_id uuid references servicos(id), data_hora timestamptz not null, status text default 'confirmado', criado_em timestamptz default now()); create table if not exists vendas (id uuid primary key default gen_random_uuid(), cliente_id uuid references clientes(id), produto_id uuid references produtos(id), quantidade integer default 1, valor_total numeric, status text default 'pago', criado_em timestamptz default now()); create table if not exists caixa (id uuid primary key default gen_random_uuid(), tipo text not null, descricao text, valor numeric not null, criado_em timestamptz default now()); create table if not exists comissoes (id uuid primary key default gen_random_uuid(), profissional_id uuid references profissionais(id), referencia text, valor numeric not null, criado_em timestamptz default now()); create table if not exists logs (id uuid primary key default gen_random_uuid(), tipo text, origem text, referencia text, conteudo jsonb, criado_em timestamptz default now());"
      },
      "id": "Supabase_Tabelas",
      "name": "Criar Tabelas Supabase",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        600,
        600
      ]
    },
    {
      "parameters": {
        "cronExpression": "*/5 * * * *"
      },
      "id": "Cron_Lembretes",
      "name": "Agendar Lembretes",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        240,
        900
      ]
    },
    {
      "parameters": {
        "calendar": "Primary",
        "start": "={{$json[\"data_hora\"]}}",
        "end": "={{$json[\"data_hora\"]}}",
        "summary": "={{$json[\"servico\"]}}",
        "description": "Agendamento autom\u00e1tico via Sal\u00e3o Inteligente"
      },
      "id": "Google_Calendar",
      "name": "Google Calendar Agendar",
      "type": "n8n-nodes-base.googleCalendar",
      "typeVersion": 2,
      "position": [
        960,
        600
      ]
    },
    {
      "parameters": {
        "functionCode": "const valor = $json.valor_total || 0;\nconst tipo = $json.tipo || 'servi\u00e7o';\nconst comissao = tipo === 'servi\u00e7o' ? valor * 0.4 : valor * 0.15;\nreturn [{...$json, comissao}];"
      },
      "id": "Calculo_Comissao",
      "name": "Calcular Comiss\u00e3o",
      "type": "n8n-nodes-base.function",
      "typeVersion": 2,
      "position": [
        1200,
        600
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "schema": "public",
        "table": "logs",
        "fields": {
          "tipo": "{{$json.tipo}}",
          "origem": "{{$json.origem}}",
          "referencia": "{{$json.referencia}}",
          "conteudo": "={{$json}}"
        }
      },
      "id": "Supabase_Logs",
      "name": "Registrar Logs Supabase",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        1500,
        600
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "schema": "public",
        "table": "comissoes",
        "fields": {
          "profissional_id": "={{$json.profissional_id}}",
          "referencia": "={{$json.id}}",
          "valor": "={{$json.comissao}}"
        }
      },
      "id": "Supabase_Comissao",
      "name": "Registrar Comiss\u00e3o",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        1800,
        600
      ]
    }
  ],
  "connections": {
    "Webhook_Whatsapp": {
      "main": [
        [
          {
            "node": "OpenAI_LLM",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI_LLM": {
      "main": [
        [
          {
            "node": "Evolution_Send",
            "type": "main",
            "index": 0
          },
          {
            "node": "Supabase_Logs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Supabase_Tabelas": {
      "main": [
        [
          {
            "node": "Webhook_Admin",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google_Calendar": {
      "main": [
        [
          {
            "node": "Calculo_Comissao",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculo_Comissao": {
      "main": [
        [
          {
            "node": "Supabase_Comissao",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Supabase_Comissao": {
      "main": [
        [
          {
            "node": "Supabase_Logs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "timezone": "America/Sao_Paulo"
  },
  "versionId": "1.0.3",
  "meta": {
    "template": "SalaoInteligenteCompleto",
    "author": "N8N Assistant by Nskha"
  }
}