version: '3.8'

# ═══════════════════════════════════════════════════════════
# RAG Document Retrieval Agent — Docker Stack
# Services: n8n (agent platform), PostgreSQL (task logging),
#           Redis (cache + idempotency)
# External integrations: Google Drive, Google Sheets,
#                        Google Gemini (via n8n credentials)
# ═══════════════════════════════════════════════════════════

services:
  # ─────────────────────────────────────────────
  # Service 1: n8n — Agent Platform
  # Hosts the RAG Document Retrieval Agent with
  # Guardrails workflow. Connects to Google Drive
  # for document ingestion, Google Sheets for
  # episodic memory + logging, and Google Gemini
  # for embeddings and chat.
  # ─────────────────────────────────────────────
  n8n:
    image: n8nio/n8n
    container_name: agent-n8n
    ports:
      - "5678:5678"
    environment:
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER=${N8N_USER}
      - N8N_BASIC_AUTH_PASSWORD=${N8N_PASSWORD}
      - GENERIC_TIMEZONE=America/New_York
      - N8N_SECURE_COOKIE=false
      - N8N_RUNNERS_ENABLED=true
      - N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true
      # Connect n8n to Postgres for persistent workflow storage
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=agent_db
      - DB_POSTGRESDB_USER=${POSTGRES_USER}
      - DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD}
    volumes:
      - n8n_data:/home/node/.n8n
      - ./workflows:/home/node/workflows:ro
    networks:
      - agent-network
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://localhost:5678/healthz || exit 1"]
      interval: 15s
      timeout: 5s
      retries: 5
      start_period: 30s
    restart: unless-stopped

  # ─────────────────────────────────────────────
  # Service 2: PostgreSQL — Structured Data Store
  # Stores n8n workflow data, execution history,
  # and agent task logs. Works alongside Google
  # Sheets (episodic memory) and Simple Vector
  # Store (semantic memory / in-memory).
  # ─────────────────────────────────────────────
  postgres:
    image: postgres:15-alpine
    container_name: agent-postgres
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=agent_db
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    networks:
      - agent-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d agent_db"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    restart: unless-stopped

  # ─────────────────────────────────────────────
  # Service 3: Redis — Cache + Idempotency Store
  # Caches frequent query results to reduce
  # redundant Gemini API calls. Stores SHA-256
  # idempotency keys (24hr TTL) to prevent
  # duplicate operations when the same request
  # is submitted twice.
  # ─────────────────────────────────────────────
  redis:
    image: redis:alpine
    container_name: agent-redis
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    networks:
      - agent-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5
      start_period: 5s
    restart: unless-stopped

networks:
  agent-network:
    driver: bridge

volumes:
  n8n_data:
  postgres_data:
  redis_data:
