version: '3.8'

services:
  # Paperclip main application
  paperclip:
    image: paperclipai/paperclip:latest
    container_name: ezygas-paperclip
    restart: always
    ports:
      - "3000:3000"
    environment:
      NODE_ENV: production
      DATABASE_URL: postgresql://paperclip:paperclip_secure_password@postgres:5432/ezygas
      VECTOR_DB_URL: http://weaviate:8080
      LOG_LEVEL: info
      PAPERCLIP_SECRET_KEY: ${PAPERCLIP_SECRET_KEY}
      ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY}
    depends_on:
      - postgres
      - weaviate
    volumes:
      - ./data/paperclip-config:/app/config
      - ./logs:/app/logs
    networks:
      - ezygas-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    # Resource limits for 2 vCPU system
    deploy:
      resources:
        limits:
          cpus: '1.5'
          memory: 4G
        reservations:
          cpus: '1'
          memory: 2G

  # PostgreSQL Database
  postgres:
    image: postgres:16-alpine
    container_name: ezygas-postgres
    restart: always
    environment:
      POSTGRES_USER: paperclip
      POSTGRES_PASSWORD: paperclip_secure_password
      POSTGRES_DB: ezygas
      POSTGRES_INITDB_ARGS: "-c max_connections=200 -c shared_buffers=256MB"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./backups:/backups
    networks:
      - ezygas-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U paperclip"]
      interval: 10s
      timeout: 5s
      retries: 5
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 1G
        reservations:
          cpus: '0.25'
          memory: 512M

  # Weaviate Vector Database (for agent memory & skills indexing)
  weaviate:
    image: semitechnologies/weaviate:latest
    container_name: ezygas-weaviate
    restart: always
    ports:
      - "8081:8080"
    environment:
      QUERY_DEFAULTS_LIMIT: 25
      AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED: 'false'
      AUTHENTICATION_APIKEY_ENABLED: 'true'
      AUTHENTICATION_APIKEY_ALLOWED_KEYS: 'ezygas-api-key'
      AUTHENTICATION_APIKEY_USERS: 'admin'
      PERSISTENCE_DATA_PATH: '/var/lib/weaviate'
    volumes:
      - weaviate_data:/var/lib/weaviate
    networks:
      - ezygas-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/v1/.well-known/ready"]
      interval: 30s
      timeout: 10s
      retries: 3
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 1.5G
        reservations:
          cpus: '0.25'
          memory: 768M

  # Nginx Reverse Proxy
  nginx:
    image: nginx:alpine
    container_name: ezygas-nginx
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./ssl:/etc/nginx/ssl
      - ./html:/usr/share/nginx/html
    depends_on:
      - paperclip
    networks:
      - ezygas-network
    healthcheck:
      test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3
    deploy:
      resources:
        limits:
          cpus: '0.25'
          memory: 512M

  # Backup service (runs daily)
  backup:
    image: postgres:16-alpine
    container_name: ezygas-backup
    restart: always
    environment:
      PGPASSWORD: paperclip_secure_password
    volumes:
      - ./backups:/backups
      - ./backup.sh:/backup.sh:ro
    entrypoint: /bin/sh -c "while true; do /backup.sh; sleep 86400; done"
    depends_on:
      - postgres
    networks:
      - ezygas-network
    deploy:
      resources:
        limits:
          cpus: '0.25'
          memory: 256M

volumes:
  postgres_data:
    driver: local
  weaviate_data:
    driver: local

networks:
  ezygas-network:
    driver: bridge
