services:
  # Redis
  redis:
    image: redis:7-alpine
    container_name: saas_redis
    restart: unless-stopped
    command:
      - redis-server
      - --appendonly
      - "yes"
      - --requirepass
      - ${REDIS_PASSWORD}
    expose:
      - "6379"
    volumes:
      - redis_data:/data
    networks:
      - saas_network
    deploy:
      resources:
        limits:
          memory: 512M

  # RabbitMQ
  rabbitmq:
    image: rabbitmq:3-management-alpine
    container_name: saas_rabbitmq
    restart: unless-stopped
    environment:
      RABBITMQ_DEFAULT_USER: ${RABBITMQ_USER}
      RABBITMQ_DEFAULT_PASS: ${RABBITMQ_PASSWORD}
    expose:
      - "5672"
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
    networks:
      - saas_network
    deploy:
      resources:
        limits:
          memory: 512M

  # Evolution API
  evolution-api:
    image: evoapicloud/evolution-api:2.4.0-rc2
    extra_hosts:
      - "host.docker.internal:host-gateway"
    container_name: saas_evolution_api
    restart: unless-stopped
    depends_on:
      redis:
        condition: service_started
      rabbitmq:
        condition: service_started
    ports:
      - "${EVOLUTION_PORT}:8080"
    environment:
      SERVER_URL: ${SERVER_URL}

      AUTHENTICATION_TYPE: apikey
      AUTHENTICATION_API_KEY: ${EVOLUTION_API_KEY}

      DATABASE_PROVIDER: mysql
      DATABASE_CONNECTION_URI: mysql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}

      CACHE_REDIS_ENABLED: "true"
      CACHE_REDIS_URI: redis://:${REDIS_PASSWORD}@redis:6379/0
      CACHE_REDIS_PREFIX_KEY: evolution

      RABBITMQ_ENABLED: "true"
      RABBITMQ_URI: amqp://${RABBITMQ_USER}:${RABBITMQ_PASSWORD}@rabbitmq:5672
      RABBITMQ_GLOBAL_ENABLED: "true"
      RABBITMQ_EVENTS_MESSAGES_UPSERT: "true"
      RABBITMQ_EVENTS_MESSAGES_UPDATE: "true"
      RABBITMQ_EVENTS_CONNECTION_UPDATE: "true"
      RABBITMQ_EVENTS_QRCODE_UPDATED: "true"

      STORE_MESSAGES: "true"
      STORE_MESSAGE_UP: "true"
      STORE_CONTACTS: "true"
      STORE_CHATS: "true"

      CLEAN_IGNORE_HISTORY: "true"
      SYNC_CONVERSATIONS: "true"
      SYNC_CONTACTS: "true"
      ONLY_SEND_RECIPIENT_MESSAGE: "true"

    volumes:
      - evolution_instances:/evolution/instances

    networks:
      - saas_network

    deploy:
      resources:
        limits:
          memory: 2G

  # n8n
  n8n:
    image: docker.n8n.io/n8nio/n8n:2.29.10
    extra_hosts:
      - "host.docker.internal:host-gateway"
    container_name: saas_n8n
    restart: unless-stopped
    ports:
      - "${N8N_PORT}:5678"
    environment:
      N8N_BLOCK_ENV_ACCESS_IN_NODE: false
      N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS: "true"

      DB_TYPE: sqlite
      DB_SQLITE_DATABASE: /home/node/.n8n/database.sqlite

      N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
      N8N_HOST: ${N8N_HOST}
      N8N_PROTOCOL: ${N8N_PROTOCOL}
      WEBHOOK_URL: ${WEBHOOK_URL}
      GENERIC_TIMEZONE: ${TIMEZONE}

      NODE_FUNCTION_ALLOW_BUILTIN: "*"
      NODE_FUNCTION_ALLOW_EXTERNAL: "*"

      GOOGLE_CLIENT_ID: ${GOOGLE_CLIENT_ID}
      GOOGLE_CLIENT_SECRET: ${GOOGLE_CLIENT_SECRET}
      # WEBHOOK_URL: https://n8n.tudominio.com

    volumes:
      - n8n_data:/home/node/.n8n

    networks:
      - saas_network

    deploy:
      resources:
        limits:
          memory: 1.5G

  # express
  control-api:
    extra_hosts:
      - "host.docker.internal:host-gateway"
    build:
      context: ./express-api
    container_name: saas_control_api
    restart: unless-stopped

    ports:
      - "${EXPRESS_PORT}:3000"

    environment:
      PORT: 3000
      DB_HOST: ${DB_HOST}
      DB_PORT: ${DB_PORT}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}
      DB_NAME: ${DB_NAME}
      SERVER_URL: ${SERVER_URL}
      EVOLUTION_API_URL: ${EVOLUTION_API_URL}
      EVOLUTION_API_KEY: ${EVOLUTION_API_KEY}
      GOOGLE_CLIENT_ID: ${GOOGLE_CLIENT_ID}
      GOOGLE_CLIENT_SECRET: ${GOOGLE_CLIENT_SECRET}
      GOOGLE_REDIRECT_URI: ${BASE_URL_EXPRESS}/api/auth/google/callback
      RABBITMQ_URL: amqp://${RABBITMQ_USER}:${RABBITMQ_PASSWORD}@rabbitmq:5672

    networks:
      - saas_network

volumes:
  redis_data:
    name: agenteia_redis_data

  rabbitmq_data:
    name: agenteia_rabbitmq_data

  evolution_instances:
    name: agenteia_evolution_instances

  n8n_data:
    name: agenteia_n8n_data

networks:
  saas_network:
    driver: bridge
