version: '3.8'

services:
  # Application
  app:
    build:
      context: .
      dockerfile: Dockerfile
      target: production
    container_name: suggestbet-app
    restart: unless-stopped
    ports:
      - '${PORT:-3000}:3000'
    environment:
      - NODE_ENV=production
      - DATABASE_URL=postgresql://${POSTGRES_USER:-suggestbet}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-boilerplate_db}?schema=public
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_PASSWORD=${REDIS_PASSWORD}
      - AI_ENGINE_URL=http://ai-engine:8000
    env_file:
      - .env
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    command: node dist/main.js
    networks:
      - suggestbet-network

  # PostgreSQL (Switched from postgresml due to disk space issues on host)
  postgres:
    image: postgres:17-alpine
    container_name: suggestbet-postgres
    restart: unless-stopped
    ports:
      - '127.0.0.1:15432:5432'
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-suggestbet}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-SuGGesT2026SecuRe}
      POSTGRES_DB: ${POSTGRES_DB:-boilerplate_db}
    volumes:
      - pgml_data:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U suggestbet -d boilerplate_db']
      interval: 10s
      timeout: 10s
      retries: 10
      start_period: 30s
    networks:
      - suggestbet-network

  # Redis
  redis:
    image: redis:7-alpine
    container_name: suggestbet-redis
    restart: unless-stopped
    ports:
      - '127.0.0.1:6379:6379' # Only localhost can access
    command: redis-server --requirepass ${REDIS_PASSWORD}
    volumes:
      - ./data/redis:/data
    healthcheck:
      test: ['CMD', 'redis-cli', 'ping']
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - suggestbet-network

  # Adminer (Database UI)
  adminer:
    image: adminer:latest
    container_name: suggestbet-adminer
    profiles:
      - dev
    restart: unless-stopped
    ports:
      - '8080:8080'
    depends_on:
      - postgres
    networks:
      - suggestbet-network

  # AI Engine (Python FastAPI)
  ai-engine:
    build:
      context: ./ai-engine
      dockerfile: Dockerfile
    container_name: suggestbet-ai-engine
    restart: unless-stopped
    ports:
      - '8002:8000'
    environment:
      - DATABASE_URL=postgresql://${POSTGRES_USER:-suggestbet}:${POSTGRES_PASSWORD:-SuGGesT2026SecuRe}@postgres:5432/${POSTGRES_DB:-boilerplate_db}?schema=public
    volumes:
      - ./ai-engine:/app
    depends_on:
      postgres:
        condition: service_healthy
    command: uvicorn main:app --host 0.0.0.0 --port 8000 --reload
    networks:
      - suggestbet-network

networks:
  suggestbet-network:
    driver: bridge

volumes:
  pgml_data:
    driver: local
