# n8n Workflow Automation - Docker Compose Override
# Use with: docker compose -f docker-compose.yml -f docker-compose.n8n.yml up -d

services:
  # Extend db service to add n8n database initialization
  db:
    environment:
      # The n8n role's own password (never the superuser's), for n8n-init.sh.
      N8N_DB_PASSWORD: ${N8N_DB_PASSWORD:?N8N_DB_PASSWORD missing, run vibecarbon upgrade}
    volumes:
      - ./volumes/db/n8n-init.sh:/docker-entrypoint-initdb.d/zz-n8n-init.sh:Z

  n8n:
    image: n8nio/n8n:2.46.6@sha256:8602f5dfacbe2c931eeafd010de0e68182310e9f309921e780ab69182e92b3ae
    container_name: ${PROJECT_NAME}-n8n
    restart: unless-stopped
    ports:
      - "5678:5678"
    environment:
      # n8n Configuration
      - N8N_HOST=0.0.0.0
      - N8N_PORT=5678
      - N8N_PROTOCOL=http
      - N8N_SECURE_COOKIE=false
      - NODE_ENV=production
      - WEBHOOK_URL=http://localhost:5678/
      - GENERIC_TIMEZONE=UTC
      # ForwardAuth SSO hook
      - EXTERNAL_HOOK_FILES=/home/node/.n8n/hooks.js

      # Database Configuration (n8n workflow storage)
      # Uses dedicated 'n8n' user created by n8n-init.sh
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=db
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=n8n
      - DB_POSTGRESDB_USER=n8n
      - DB_POSTGRESDB_PASSWORD=${N8N_DB_PASSWORD:?N8N_DB_PASSWORD missing, run vibecarbon upgrade}

      # One key per environment (generated by `vibecarbon add n8n`, healed by
      # `vibecarbon upgrade`), so a promoted standby can read replicated credentials
      - N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY:?N8N_ENCRYPTION_KEY missing, run vibecarbon upgrade}

      # App database (for workflows), for the n8n Postgres node.
      # The n8n role, never the superuser: n8n-init.sh makes it NOSUPERUSER
      # BYPASSRLS with read and write on the business tables and no access
      # to the server-only tables. Its password is its own, N8N_DB_PASSWORD.
      - APP_DB_HOST=db
      - APP_DB_PORT=5432
      - APP_DB_NAME=postgres
      - APP_DB_USER=n8n
      - APP_DB_PASSWORD=${N8N_DB_PASSWORD:?N8N_DB_PASSWORD missing, run vibecarbon upgrade}

      # Queue mode (requires Redis — added automatically with n8n)
      - EXECUTIONS_MODE=queue
      - QUEUE_BULL_REDIS_HOST=redis
      - QUEUE_BULL_REDIS_PORT=6379
      - QUEUE_BULL_REDIS_PASSWORD=${REDIS_PASSWORD:-}

      # Metrics for Prometheus (optional)
      - N8N_METRICS=true
      - N8N_METRICS_PREFIX=n8n_
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:5678/healthz || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s
    volumes:
      - n8n_data:/home/node/.n8n
      - ./volumes/n8n/hooks.js:/home/node/.n8n/hooks.js:ro
    depends_on:
      db:
        condition: service_healthy
    networks:
      - vibecarbon-network
    labels:
      # Traefik routing with ForwardAuth (super_admin only)
      - "traefik.enable=true"
      - "traefik.http.routers.n8n.rule=Host(`n8n.localhost`)"
      - "traefik.http.routers.n8n.entrypoints=web"
      - "traefik.http.routers.n8n.middlewares=super-admin-auth@file"
      - "traefik.http.services.n8n.loadbalancer.server.port=5678"

  n8n-setup:
    image: alpine:3.24.4@sha256:93b41ef40b1677fce8fcc64ff4af09daddfacf5040afde618bc46c1e2d1cc7d4
    container_name: ${PROJECT_NAME}-n8n-setup
    volumes:
      - ./volumes/n8n/scripts:/scripts:ro
    environment:
      - N8N_ADMIN_EMAIL=${N8N_ADMIN_EMAIL:-${ADMIN_EMAIL}}
      - N8N_DB_HOST=db
      - N8N_DB_NAME=n8n
      - N8N_DB_USER=n8n
      - N8N_DB_PASSWORD=${N8N_DB_PASSWORD:?N8N_DB_PASSWORD missing, run vibecarbon upgrade}
    entrypoint: ["/bin/sh", "-c", "apk add --no-cache postgresql16-client && sh /scripts/setup.sh"]
    depends_on:
      db:
        condition: service_healthy
    networks:
      - vibecarbon-network
    restart: "no"

volumes:
  n8n_data:
