version: '3.8'

networks:
  n8n_network:
    driver: bridge

services:
  redis:
    image: redis:7-alpine
    platform: linux/amd64
    command: redis-server --requirepass ${REDIS_PASSWORD:-n8n_redis_2024!}
    environment:
      REDIS_PASSWORD: ${REDIS_PASSWORD:-n8n_redis_2024!}
    volumes:
      - redis_data:/data
    networks:
      - n8n_network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3

  postgres:
    image: postgres:15-alpine
    platform: linux/amd64
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-n8n_user}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-n8n_postgres_2024!}
      POSTGRES_DB: ${POSTGRES_DB:-n8n_db}
      PGDATA: /var/lib/postgresql/data/pgdata
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks:
      - n8n_network
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-n8n_user} -d ${POSTGRES_DB:-n8n_db}"]
      interval: 30s
      timeout: 10s
      retries: 3

  waha:
    image: devlikeapro/waha:latest
    platform: linux/amd64
    environment:
      # Usando nome do serviço em vez de IP fixo
      WHATSAPP_HOOK_URL: http://n8n:5678/webhook/webhook
      WHATSAPP_DEFAULT_ENGINE: GOWS
      WHATSAPP_HOOK_EVENTS: message
    volumes:
      - waha_sessions:/app/.sessions
      - waha_media:/app/.media
    ports:
      - "${WAHA_PORT:-3000}:3000"
    networks:
      - n8n_network
    restart: unless-stopped
    depends_on:
      - n8n

  n8n:
    image: n8nio/n8n:latest
    platform: linux/amd64
    environment:
      # Configurações gerais
      GENERIC_TIMEZONE: America/Sao_Paulo
      N8N_LOG_LEVEL: info
      N8N_COMMUNITY_PACKAGES_ALLOW_TOOL_USAGE: true

      # URLs e Host - configurado para seu VPS
      WEBHOOK_URL: http://104.161.20.130:5678
      N8N_HOST: 0.0.0.0
      N8N_PORT: 5678
      N8N_PROTOCOL: http

      # Configuração do PostgreSQL
      DB_TYPE: postgresdb
      DB_POSTGRESDB_HOST: postgres
      DB_POSTGRESDB_PORT: 5432
      DB_POSTGRESDB_DATABASE: ${POSTGRES_DB:-n8n_db}
      DB_POSTGRESDB_USER: ${POSTGRES_USER:-n8n_user}
      DB_POSTGRESDB_PASSWORD: ${POSTGRES_PASSWORD:-n8n_postgres_2024!}
      DB_POSTGRESDB_SCHEMA: public

      # Configuração do Redis (opcional, para cache/queue)
      QUEUE_BULL_REDIS_HOST: redis
      QUEUE_BULL_REDIS_PORT: 6379
      QUEUE_BULL_REDIS_PASSWORD: ${REDIS_PASSWORD:-n8n_redis_2024!}
      QUEUE_BULL_REDIS_DB: 0

      # Configurações de execução
      EXECUTIONS_MODE: queue
      EXECUTIONS_DATA_PRUNE: true
      EXECUTIONS_DATA_MAX_AGE: 168

      # Configurações de segurança
      N8N_BASIC_AUTH_ACTIVE: ${N8N_BASIC_AUTH_ACTIVE:-false}
      N8N_BASIC_AUTH_USER: ${N8N_BASIC_AUTH_USER:-admin}
      N8N_BASIC_AUTH_PASSWORD: ${N8N_BASIC_AUTH_PASSWORD:-admin}

    volumes:
      - n8n_data:/home/node/.n8n
    ports:
      - "${N8N_PORT:-5678}:5678"
    networks:
      - n8n_network
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost:5678/healthz"]
      interval: 30s
      timeout: 10s
      retries: 3

volumes:
  pgdata:
    driver: local
  redis_data:
    driver: local
  waha_sessions:
    driver: local
  waha_media:
    driver: local
  n8n_data:
    driver: local