version: '3.8'

services:
  n8n:
    image: n8nio/n8n:latest
    container_name: n8n-shared
    restart: unless-stopped
    networks:
      - traefik-public
      - database-internal
      - n8n-shared
    env_file:
      - .env
    environment:
      # General
      N8N_HOST: n8n.ai-impress.com
      N8N_PORT: 5678
      N8N_PROTOCOL: https
      WEBHOOK_URL: https://n8n.ai-impress.com/

      # Database
      DB_TYPE: postgresdb
      DB_POSTGRESDB_HOST: postgres-main
      DB_POSTGRESDB_PORT: 5432
      DB_POSTGRESDB_DATABASE: n8n_shared
      DB_POSTGRESDB_USER: n8n_user
      DB_POSTGRESDB_PASSWORD: ${DB_PASSWORD}
      DB_POSTGRESDB_POOL_SIZE: 10

      # Queue Mode (Redis) & Task Runners
      QUEUE_BULL_REDIS_HOST: redis-main
      QUEUE_BULL_REDIS_PORT: 6379
      QUEUE_BULL_REDIS_PASSWORD: ${REDIS_PASSWORD}
      QUEUE_BULL_REDIS_DB: 2
      EXECUTIONS_MODE: queue
      N8N_RUNNERS_ENABLED: 'true'

      # Workers configuration
      QUEUE_HEALTH_CHECK_ACTIVE: 'true'

      # Security
      N8N_BASIC_AUTH_ACTIVE: 'false'
      N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
      N8N_PAYLOAD_SIZE_MAX: 64
      N8N_BLOCK_ENV_ACCESS_IN_NODE: 'false'

      # Community Packages
      N8N_COMMUNITY_PACKAGES_ALLOW_TOOL_USAGE: 'true'

      # Performance
      EXECUTIONS_DATA_PRUNE: 'true'
      EXECUTIONS_DATA_MAX_AGE: 168
      N8N_CONCURRENCY_PRODUCTION_LIMIT: 5
      
      # Timezone
      GENERIC_TIMEZONE: Europe/London
      TZ: Europe/London
      
    volumes:
      - n8n-data:/home/node/.n8n
      - ./custom-nodes:/home/node/.n8n/custom
    deploy:
      resources:
        limits:
          cpus: '4'
          memory: 2G
        reservations:
          cpus: '1'
          memory: 512M
    labels:
      - "traefik.enable=true"
      
      # Webhook роутер БЕЗ Authelia
      - "traefik.http.routers.n8n-webhook.rule=Host(`n8n.ai-impress.com`) && (PathPrefix(`/webhook/`) || PathPrefix(`/webhook-test/`))"
      - "traefik.http.routers.n8n-webhook.entrypoints=websecure"
      - "traefik.http.routers.n8n-webhook.tls.certresolver=cloudflare"
      - "traefik.http.routers.n8n-webhook.service=n8n-shared-service"
      - "traefik.http.routers.n8n-webhook.priority=200"
      
      # OAuth роутер БЕЗ Authelia
      - "traefik.http.routers.n8n-oauth.rule=Host(`n8n.ai-impress.com`) && PathPrefix(`/rest/oauth`)"
      - "traefik.http.routers.n8n-oauth.entrypoints=websecure"
      - "traefik.http.routers.n8n-oauth.tls.certresolver=cloudflare"
      - "traefik.http.routers.n8n-oauth.service=n8n-shared-service"
      - "traefik.http.routers.n8n-oauth.priority=150"
      
      # Основной роутер БЕЗ Authelia
      - "traefik.http.routers.n8n-shared.rule=Host(`n8n.ai-impress.com`)"
      - "traefik.http.routers.n8n-shared.entrypoints=websecure"
      - "traefik.http.routers.n8n-shared.tls.certresolver=cloudflare"
      - "traefik.http.routers.n8n-shared.service=n8n-shared-service"
      - "traefik.http.routers.n8n-shared.priority=50"
      
      # Сервис
      - "traefik.http.services.n8n-shared-service.loadbalancer.server.port=5678"

  # Worker 1 - High concurrency для быстрых задач
  n8n-worker-1:
    image: n8nio/n8n:latest
    container_name: n8n-worker-1
    restart: unless-stopped
    command: worker
    networks:
      - database-internal
      - n8n-shared
    env_file:
      - .env
    environment:
      # Database
      DB_TYPE: postgresdb
      DB_POSTGRESDB_HOST: postgres-main
      DB_POSTGRESDB_PORT: 5432
      DB_POSTGRESDB_DATABASE: n8n_shared
      DB_POSTGRESDB_USER: n8n_user
      DB_POSTGRESDB_PASSWORD: ${DB_PASSWORD}
      DB_POSTGRESDB_POOL_SIZE: 5

      # Queue Mode (Redis) & Task Runners
      QUEUE_BULL_REDIS_HOST: redis-main
      QUEUE_BULL_REDIS_PORT: 6379
      QUEUE_BULL_REDIS_PASSWORD: ${REDIS_PASSWORD}
      QUEUE_BULL_REDIS_DB: 2
      EXECUTIONS_MODE: queue
      N8N_RUNNERS_ENABLED: 'true'

      # Worker settings
      N8N_CONCURRENCY_PRODUCTION_LIMIT: 20

      # Security
      N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
      N8N_BLOCK_ENV_ACCESS_IN_NODE: 'false'

      # Community Packages
      N8N_COMMUNITY_PACKAGES_ALLOW_TOOL_USAGE: 'true'
      
      # Timezone
      GENERIC_TIMEZONE: Europe/London
      TZ: Europe/London
      
    volumes:
      - n8n-data:/home/node/.n8n
      - ./custom-nodes:/home/node/.n8n/custom
    deploy:
      resources:
        limits:
          cpus: '4'
          memory: 4G
        reservations:
          cpus: '2'
          memory: 1G

  # Worker 2 - High concurrency
  n8n-worker-2:
    image: n8nio/n8n:latest
    container_name: n8n-worker-2
    restart: unless-stopped
    command: worker
    networks:
      - database-internal
      - n8n-shared
    env_file:
      - .env
    environment:
      # Database
      DB_TYPE: postgresdb
      DB_POSTGRESDB_HOST: postgres-main
      DB_POSTGRESDB_PORT: 5432
      DB_POSTGRESDB_DATABASE: n8n_shared
      DB_POSTGRESDB_USER: n8n_user
      DB_POSTGRESDB_PASSWORD: ${DB_PASSWORD}
      DB_POSTGRESDB_POOL_SIZE: 5

      # Queue Mode (Redis) & Task Runners
      QUEUE_BULL_REDIS_HOST: redis-main
      QUEUE_BULL_REDIS_PORT: 6379
      QUEUE_BULL_REDIS_PASSWORD: ${REDIS_PASSWORD}
      QUEUE_BULL_REDIS_DB: 2
      EXECUTIONS_MODE: queue
      N8N_RUNNERS_ENABLED: 'true'

      # Worker settings
      N8N_CONCURRENCY_PRODUCTION_LIMIT: 20

      # Security
      N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
      N8N_BLOCK_ENV_ACCESS_IN_NODE: 'false'

      # Community Packages
      N8N_COMMUNITY_PACKAGES_ALLOW_TOOL_USAGE: 'true'
      
      # Timezone
      GENERIC_TIMEZONE: Europe/London
      TZ: Europe/London
      
    volumes:
      - n8n-data:/home/node/.n8n
      - ./custom-nodes:/home/node/.n8n/custom
    deploy:
      resources:
        limits:
          cpus: '4'
          memory: 4G
        reservations:
          cpus: '2'
          memory: 1G

  # Worker 3 - Medium concurrency для сложных задач
  n8n-worker-3:
    image: n8nio/n8n:latest
    container_name: n8n-worker-3
    restart: unless-stopped
    command: worker
    networks:
      - database-internal
      - n8n-shared
    env_file:
      - .env
    environment:
      # Database
      DB_TYPE: postgresdb
      DB_POSTGRESDB_HOST: postgres-main
      DB_POSTGRESDB_PORT: 5432
      DB_POSTGRESDB_DATABASE: n8n_shared
      DB_POSTGRESDB_USER: n8n_user
      DB_POSTGRESDB_PASSWORD: ${DB_PASSWORD}
      DB_POSTGRESDB_POOL_SIZE: 5

      # Queue Mode (Redis) & Task Runners
      QUEUE_BULL_REDIS_HOST: redis-main
      QUEUE_BULL_REDIS_PORT: 6379
      QUEUE_BULL_REDIS_PASSWORD: ${REDIS_PASSWORD}
      QUEUE_BULL_REDIS_DB: 2
      EXECUTIONS_MODE: queue
      N8N_RUNNERS_ENABLED: 'true'

      # Worker settings
      N8N_CONCURRENCY_PRODUCTION_LIMIT: 15

      # Security
      N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
      N8N_BLOCK_ENV_ACCESS_IN_NODE: 'false'

      # Community Packages
      N8N_COMMUNITY_PACKAGES_ALLOW_TOOL_USAGE: 'true'
      
      # Timezone
      GENERIC_TIMEZONE: Europe/London
      TZ: Europe/London
      
    volumes:
      - n8n-data:/home/node/.n8n
      - ./custom-nodes:/home/node/.n8n/custom
    deploy:
      resources:
        limits:
          cpus: '3'
          memory: 3G
        reservations:
          cpus: '1'
          memory: 1G

  # Worker 4 - Medium concurrency
  n8n-worker-4:
    image: n8nio/n8n:latest
    container_name: n8n-worker-4
    restart: unless-stopped
    command: worker
    networks:
      - database-internal
      - n8n-shared
    env_file:
      - .env
    environment:
      # Database
      DB_TYPE: postgresdb
      DB_POSTGRESDB_HOST: postgres-main
      DB_POSTGRESDB_PORT: 5432
      DB_POSTGRESDB_DATABASE: n8n_shared
      DB_POSTGRESDB_USER: n8n_user
      DB_POSTGRESDB_PASSWORD: ${DB_PASSWORD}
      DB_POSTGRESDB_POOL_SIZE: 5

      # Queue Mode (Redis) & Task Runners
      QUEUE_BULL_REDIS_HOST: redis-main
      QUEUE_BULL_REDIS_PORT: 6379
      QUEUE_BULL_REDIS_PASSWORD: ${REDIS_PASSWORD}
      QUEUE_BULL_REDIS_DB: 2
      EXECUTIONS_MODE: queue
      N8N_RUNNERS_ENABLED: 'true'

      # Worker settings
      N8N_CONCURRENCY_PRODUCTION_LIMIT: 15

      # Security
      N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
      N8N_BLOCK_ENV_ACCESS_IN_NODE: 'false'

      # Community Packages
      N8N_COMMUNITY_PACKAGES_ALLOW_TOOL_USAGE: 'true'
      
      # Timezone
      GENERIC_TIMEZONE: Europe/London
      TZ: Europe/London
      
    volumes:
      - n8n-data:/home/node/.n8n
      - ./custom-nodes:/home/node/.n8n/custom
    deploy:
      resources:
        limits:
          cpus: '3'
          memory: 3G
        reservations:
          cpus: '1'
          memory: 1G

volumes:
  n8n-data:

networks:
  traefik-public:
    external: true
  database-internal:
    external: true
  n8n-shared:
    external: true
