services:
  # PostgreSQL Database
  postgres:
    image: postgres:17-alpine
    container_name: lyra-postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${DB_USER:-postgres}
      POSTGRES_PASSWORD: ${DB_PASS:-postgres}
      POSTGRES_DB: ${DB_NAME:-lyra_db}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "${DB_PORT:-5432}:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-postgres} -d ${DB_NAME:-lyra_db}"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - lyra-network
    # Performance tuning for high load
    command:
      - "postgres"
      - "-c"
      - "max_connections=500"
      - "-c"
      - "shared_buffers=256MB"
      - "-c"
      - "effective_cache_size=768MB"
      - "-c"
      - "maintenance_work_mem=128MB"
      - "-c"
      - "checkpoint_completion_target=0.9"
      - "-c"
      - "wal_buffers=16MB"
      - "-c"
      - "default_statistics_target=100"
      - "-c"
      - "random_page_cost=1.1"
      - "-c"
      - "effective_io_concurrency=200"
      - "-c"
      - "work_mem=4MB"
      - "-c"
      - "min_wal_size=1GB"
      - "-c"
      - "max_wal_size=4GB"

  # Redis Cache
  redis:
    image: redis:7-alpine
    container_name: lyra-redis
    restart: unless-stopped
    ports:
      - "${REDIS_PORT:-6380}:6379"
    volumes:
      - redis_data:/data
    command: >
      redis-server
      --appendonly yes
      --maxmemory 512mb
      --maxmemory-policy allkeys-lru
      --tcp-keepalive 300
      --timeout 0
      --tcp-backlog 511
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - lyra-network

  # Lyra Backend API
  backend:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: lyra-backend
    restart: unless-stopped
    ports:
      - "${PORT:-9090}:${PORT:-9090}"
    environment:
      NODE_ENV: ${NODE_ENV:-production}
      PORT: ${PORT:-9090}
      # Database
      DB_HOST: postgres
      DB_PORT: 5432
      DB_USER: ${DB_USER:-postgres}
      DB_PASS: ${DB_PASS:-postgres}
      DB_NAME: ${DB_NAME:-lyra_db}
      DB_POOL_MAX: ${DB_POOL_MAX:-100}
      DB_POOL_MIN: ${DB_POOL_MIN:-10}
      DB_POOL_ACQUIRE: ${DB_POOL_ACQUIRE:-30000}
      DB_POOL_IDLE: ${DB_POOL_IDLE:-10000}
      # Redis
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_TTL: ${REDIS_TTL:-3600}
      # JWT
      JWT_SECRET: ${JWT_SECRET:-your-super-secret-jwt-key-change-in-production}
      # Agentic API
      AGENTIC_API_URL: ${AGENTIC_API_URL:-https://192.168.18.7}
      # Email settings
      MAIL_DRIVER: ${MAIL_DRIVER:-smtp}
      SMTP_HOST: ${SMTP_HOST:-smtp.hostinger.com}
      SMTP_PORT: ${SMTP_PORT:-587}
      SMTP_SECURE: ${SMTP_SECURE:-false}
      SMTP_USER: ${SMTP_USER:-code@robocodesol.com}
      SMTP_PASS: ${SMTP_PASS:-}
      MAIL_ENCRYPTION: ${MAIL_ENCRYPTION:-auto}
      SMTP_FROM: ${SMTP_FROM:-code@robocodesol.com}
      SMTP_FROM_NAME: ${SMTP_FROM_NAME:-Lyra AI}
      MAIL_FROM_NAME: ${MAIL_FROM_NAME:-Lyra AI}
      # OAuth - GitHub
      GITHUB_CLIENT_ID: ${GITHUB_CLIENT_ID:-}
      GITHUB_CLIENT_SECRET: ${GITHUB_CLIENT_SECRET:-}
      # OAuth - Google
      GOOGLE_CLIENT_ID: ${GOOGLE_CLIENT_ID:-}
      GOOGLE_CLIENT_SECRET: ${GOOGLE_CLIENT_SECRET:-}
      # OAuth URLs
      BETTER_AUTH_URL: ${BETTER_AUTH_URL:-http://localhost:9090}
      FRONTEND_URL: ${FRONTEND_URL:-http://localhost:3000}
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - lyra-network
    # For high-load scenarios, can scale horizontally
    deploy:
      resources:
        limits:
          memory: 1G
        reservations:
          memory: 512M

volumes:
  postgres_data:
    driver: local
  redis_data:
    driver: local

networks:
  lyra-network:
    driver: bridge
