# =============================================================================
# docker-compose.prod.databases.yml — Production Databases & Infrastructure
# =============================================================================
# Contains: PostgreSQL instances, MongoDB, Redis, RabbitMQ
# Creates the claw-network bridge that all other compose files reference.
#
# Reads environment variables from the root .env file.
# Copy .env.example to .env before running.
#
# Usage:
#   docker compose -f docker-compose.prod.databases.yml up -d
#   ../scripts/claw.sh --prod db:up
# =============================================================================

name: claw

services:
  # ---------------------------------------------------------------------------
  # PostgreSQL Instances (one per service that needs relational storage)
  # ---------------------------------------------------------------------------
  pg-auth:
    image: pgvector/pgvector:pg16
    container_name: claw-pg-auth
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${PG_AUTH_USER:-claw}
      POSTGRES_PASSWORD: ${PG_AUTH_PASSWORD:-claw_secret}
      POSTGRES_DB: ${PG_AUTH_DB:-claw_auth}
    ports:
      - '${PG_AUTH_PORT:-5441}:5432'
    volumes:
      - pg-auth-data:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U ${PG_AUTH_USER:-claw} -d ${PG_AUTH_DB:-claw_auth}']
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - claw-network

  pg-chat:
    image: pgvector/pgvector:pg16
    container_name: claw-pg-chat
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${PG_CHAT_USER:-claw}
      POSTGRES_PASSWORD: ${PG_CHAT_PASSWORD:-claw_secret}
      POSTGRES_DB: ${PG_CHAT_DB:-claw_chat}
    ports:
      - '${PG_CHAT_PORT:-5442}:5432'
    volumes:
      - pg-chat-data:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U ${PG_CHAT_USER:-claw} -d ${PG_CHAT_DB:-claw_chat}']
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - claw-network

  pg-connector:
    image: pgvector/pgvector:pg16
    container_name: claw-pg-connector
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${PG_CONNECTOR_USER:-claw}
      POSTGRES_PASSWORD: ${PG_CONNECTOR_PASSWORD:-claw_secret}
      POSTGRES_DB: ${PG_CONNECTOR_DB:-claw_connectors}
    ports:
      - '${PG_CONNECTOR_PORT:-5443}:5432'
    volumes:
      - pg-connector-data:/var/lib/postgresql/data
    healthcheck:
      test:
        [
          'CMD-SHELL',
          'pg_isready -U ${PG_CONNECTOR_USER:-claw} -d ${PG_CONNECTOR_DB:-claw_connectors}',
        ]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - claw-network

  pg-routing:
    image: pgvector/pgvector:pg16
    container_name: claw-pg-routing
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${PG_ROUTING_USER:-claw}
      POSTGRES_PASSWORD: ${PG_ROUTING_PASSWORD:-claw_secret}
      POSTGRES_DB: ${PG_ROUTING_DB:-claw_routing}
    ports:
      - '${PG_ROUTING_PORT:-5444}:5432'
    volumes:
      - pg-routing-data:/var/lib/postgresql/data
    healthcheck:
      test:
        ['CMD-SHELL', 'pg_isready -U ${PG_ROUTING_USER:-claw} -d ${PG_ROUTING_DB:-claw_routing}']
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - claw-network

  pg-memory:
    image: pgvector/pgvector:pg16
    container_name: claw-pg-memory
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${PG_MEMORY_USER:-claw}
      POSTGRES_PASSWORD: ${PG_MEMORY_PASSWORD:-claw_secret}
      POSTGRES_DB: ${PG_MEMORY_DB:-claw_memory}
    ports:
      - '${PG_MEMORY_PORT:-5445}:5432'
    volumes:
      - pg-memory-data:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U ${PG_MEMORY_USER:-claw} -d ${PG_MEMORY_DB:-claw_memory}']
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - claw-network

  pg-files:
    image: pgvector/pgvector:pg16
    container_name: claw-pg-files
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${PG_FILES_USER:-claw}
      POSTGRES_PASSWORD: ${PG_FILES_PASSWORD:-claw_secret}
      POSTGRES_DB: ${PG_FILES_DB:-claw_files}
    ports:
      - '${PG_FILES_PORT:-5446}:5432'
    volumes:
      - pg-files-data:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U ${PG_FILES_USER:-claw} -d ${PG_FILES_DB:-claw_files}']
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - claw-network

  pg-ollama:
    image: pgvector/pgvector:pg16
    container_name: claw-pg-ollama
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${PG_OLLAMA_USER:-claw}
      POSTGRES_PASSWORD: ${PG_OLLAMA_PASSWORD:-claw_secret}
      POSTGRES_DB: ${PG_OLLAMA_DB:-claw_ollama}
    ports:
      - '${PG_OLLAMA_PORT:-5447}:5432'
    volumes:
      - pg-ollama-data:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U ${PG_OLLAMA_USER:-claw} -d ${PG_OLLAMA_DB:-claw_ollama}']
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - claw-network

  # ---------------------------------------------------------------------------
  # PostgreSQL (Image Service)
  # ---------------------------------------------------------------------------
  pg-images:
    image: pgvector/pgvector:pg16
    container_name: claw-pg-images
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${PG_IMAGES_USER:-claw}
      POSTGRES_PASSWORD: ${PG_IMAGES_PASSWORD:-claw_secret}
      POSTGRES_DB: ${PG_IMAGES_DB:-claw_images}
    ports:
      - '${PG_IMAGES_PORT:-5448}:5432'
    volumes:
      - pg-images-data:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U ${PG_IMAGES_USER:-claw} -d ${PG_IMAGES_DB:-claw_images}']
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - claw-network

  # ---------------------------------------------------------------------------
  # PostgreSQL (Workspace Service)
  # ---------------------------------------------------------------------------
  pg-workspace:
    image: pgvector/pgvector:pg16
    container_name: claw-pg-workspace
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${PG_WORKSPACE_USER:-claw}
      POSTGRES_PASSWORD: ${PG_WORKSPACE_PASSWORD:-claw_secret}
      POSTGRES_DB: ${PG_WORKSPACE_DB:-claw_workspace}
    ports:
      - '${PG_WORKSPACE_PORT:-5450}:5432'
    volumes:
      - pg-workspace-data:/var/lib/postgresql/data
    healthcheck:
      test:
        [
          'CMD-SHELL',
          'pg_isready -U ${PG_WORKSPACE_USER:-claw} -d ${PG_WORKSPACE_DB:-claw_workspace}',
        ]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - claw-network

  # ---------------------------------------------------------------------------
  # PostgreSQL (Research Service)
  # ---------------------------------------------------------------------------
  pg-research:
    image: pgvector/pgvector:pg16
    container_name: claw-pg-research
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${PG_RESEARCH_USER:-claw}
      POSTGRES_PASSWORD: ${PG_RESEARCH_PASSWORD:-claw_secret}
      POSTGRES_DB: ${PG_RESEARCH_DB:-claw_research}
    ports:
      - '${PG_RESEARCH_PORT:-5452}:5432'
    volumes:
      - pg-research-data:/var/lib/postgresql/data
    healthcheck:
      test:
        ['CMD-SHELL', 'pg_isready -U ${PG_RESEARCH_USER:-claw} -d ${PG_RESEARCH_DB:-claw_research}']
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - claw-network

  # ---------------------------------------------------------------------------
  # PostgreSQL (Agent Service)
  # ---------------------------------------------------------------------------
  pg-agent:
    image: pgvector/pgvector:pg16
    container_name: claw-pg-agent
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${PG_AGENT_USER:-claw}
      POSTGRES_PASSWORD: ${PG_AGENT_PASSWORD:-claw_secret}
      POSTGRES_DB: ${PG_AGENT_DB:-claw_agent}
    ports:
      - '${PG_AGENT_PORT:-5451}:5432'
    volumes:
      - pg-agent-data:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U ${PG_AGENT_USER:-claw} -d ${PG_AGENT_DB:-claw_agent}']
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - claw-network

  pg-llamacpp:
    image: pgvector/pgvector:pg16
    container_name: claw-pg-llamacpp
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${PG_LLAMACPP_USER:-claw}
      POSTGRES_PASSWORD: ${PG_LLAMACPP_PASSWORD:-claw_secret}
      POSTGRES_DB: ${PG_LLAMACPP_DB:-claw_llamacpp}
    ports:
      - '${PG_LLAMACPP_PORT:-5440}:5432'
    volumes:
      - pg-llamacpp-data:/var/lib/postgresql/data
    healthcheck:
      test:
        ['CMD-SHELL', 'pg_isready -U ${PG_LLAMACPP_USER:-claw} -d ${PG_LLAMACPP_DB:-claw_llamacpp}']
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - claw-network

  # ---------------------------------------------------------------------------
  # PostgreSQL (File Generation Service)
  # ---------------------------------------------------------------------------
  pg-file-generations:
    image: pgvector/pgvector:pg16
    container_name: claw-pg-file-generations
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${PG_FILE_GENERATIONS_USER:-claw}
      POSTGRES_PASSWORD: ${PG_FILE_GENERATIONS_PASSWORD:-claw_secret}
      POSTGRES_DB: ${PG_FILE_GENERATIONS_DB:-claw_file_generations}
    ports:
      - '${PG_FILE_GENERATIONS_PORT:-5449}:5432'
    volumes:
      - pg-file-generations-data:/var/lib/postgresql/data
    healthcheck:
      test:
        [
          'CMD-SHELL',
          'pg_isready -U ${PG_FILE_GENERATIONS_USER:-claw} -d ${PG_FILE_GENERATIONS_DB:-claw_file_generations}',
        ]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - claw-network

  # ---------------------------------------------------------------------------
  # MongoDB (Audit Service)
  # ---------------------------------------------------------------------------
  mongodb:
    image: mongo:7
    container_name: claw-mongodb
    restart: unless-stopped
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_USER:-claw}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD:-claw_secret}
      MONGO_INITDB_DATABASE: ${MONGO_DB:-claw_audit}
    ports:
      - '${MONGO_PORT:-27018}:27017'
    volumes:
      - mongo-data:/data/db
    healthcheck:
      test: ['CMD', 'mongosh', '--eval', 'db.runCommand("ping").ok', '--quiet']
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - claw-network

  # ---------------------------------------------------------------------------
  # Redis
  # ---------------------------------------------------------------------------
  redis:
    image: redis:7-alpine
    container_name: claw-redis
    restart: unless-stopped
    ports:
      - '${REDIS_PORT:-6380}:6379'
    volumes:
      - redis-data:/data
    healthcheck:
      test: ['CMD', 'redis-cli', 'ping']
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - claw-network

  # ---------------------------------------------------------------------------
  # RabbitMQ
  # ---------------------------------------------------------------------------
  rabbitmq:
    image: rabbitmq:3-management-alpine
    container_name: claw-rabbitmq
    restart: unless-stopped
    environment:
      RABBITMQ_DEFAULT_USER: ${RABBITMQ_USER:-claw}
      RABBITMQ_DEFAULT_PASS: ${RABBITMQ_PASSWORD:-claw_secret}
    ports:
      - '${RABBITMQ_PORT:-5672}:5672'
      - '${RABBITMQ_MANAGEMENT_PORT:-15672}:15672'
    volumes:
      - rabbitmq-data:/var/lib/rabbitmq
    healthcheck:
      test: ['CMD-SHELL', 'rabbitmq-diagnostics -q check_running']
      interval: 15s
      timeout: 10s
      retries: 5
    networks:
      - claw-network

  # ---------------------------------------------------------------------------
  # ClamAV (Antivirus file scanning)
  # ---------------------------------------------------------------------------
  clamav:
    image: clamav/clamav-debian:stable
    container_name: claw-clamav
    restart: unless-stopped
    ports:
      - '${CLAMAV_PORT:-3310}:3310'
    volumes:
      - clamav-data:/var/lib/clamav
    environment:
      - CLAMAV_NO_FRESHCLAMD=false
    healthcheck:
      test: ['CMD-SHELL', 'clamdscan --ping 1 || exit 1']
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 120s
    networks:
      - claw-network

volumes:
  pg-auth-data:
    driver: local
  pg-chat-data:
    driver: local
  pg-connector-data:
    driver: local
  pg-routing-data:
    driver: local
  pg-memory-data:
    driver: local
  pg-files-data:
    driver: local
  pg-ollama-data:
    driver: local
  pg-images-data:
    driver: local
  pg-workspace-data:
    driver: local
  pg-research-data:
    driver: local
  pg-agent-data:
    driver: local
  pg-llamacpp-data:
    driver: local
  pg-file-generations-data:
    driver: local
  mongo-data:
    driver: local
  redis-data:
    driver: local
  rabbitmq-data:
    driver: local
  file-storage-data:
    driver: local
  clamav-data:
    driver: local

networks:
  claw-network:
    external: true
    name: claw-network
