version: '3.8'

services:
  # Database service for RPS
  gcic-db:
    image: mysql:8.0
    container_name: gcic-mysql-database
    environment:
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
    volumes:
      - gcic_mysql_data:/var/lib/mysql
    networks:
      - gcic-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${MYSQL_ROOT_PASSWORD}"]
      interval: 10s
      timeout: 10s
      retries: 10
      start_period: 30s

  # MySQL Exporter for Prometheus
  mysqld_exporter:
    image: prom/mysqld-exporter:v0.15.1
    container_name: gcic-mysqld-exporter
    command:
      - "--config.my-cnf=/etc/mysqld-exporter/.my.cnf"
    volumes:
      - ./mysqld-exporter.cnf:/etc/mysqld-exporter/.my.cnf:ro
    ports:
      - "9104:9104"
    depends_on:
      gcic-db:
        condition: service_healthy
    networks:
      - gcic-network

  # Qdrant Vector DB
  qdrant:
    image: qdrant/qdrant:v1.12.3
    container_name: qdrant-db
    ports:
      - "6333:6333"
    volumes:
      - ./data/qdrant_storage:/qdrant/storage
    networks:
      - gcic-network
    restart: unless-stopped

  # Redis
  redis:
    image: redis:7-alpine
    container_name: gcic-redis
    ports:
      - "6379:6379"
    volumes:
      - gcic_redis_data:/data
    networks:
      - gcic-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped
    command: redis-server --appendonly yes

  # Django backend
  gcic-backend:
    build: ./backend
    container_name: gcic-backend-image
    env_file:
      - .env
    command: >
      sh -c "
      echo 'Waiting for MySQL...' &&
      until nc -z gcic-db 3306; do sleep 2; done &&
      echo 'Waiting for Redis...' &&
      until nc -z redis 6379; do sleep 2; done &&
      echo 'MySQL and Redis are ready. Starting Django...' &&
      python manage.py migrate --noinput &&
      python manage.py collectstatic --noinput &&
      daphne -b 0.0.0.0 -p 8791 RPS_project.asgi:application
      "
    depends_on:
      - gcic-db
      - qdrant
      - redis
    environment:
      DB_HOST: gcic-db
      DB_NAME: ${MYSQL_DATABASE}
      DB_USER: ${MYSQL_USER}
      DB_PASSWORD: ${MYSQL_PASSWORD}
      REDIS_HOST: redis
      REDIS_PORT: 6379
      QDRANT_URL: http://qdrant:6333
    volumes:
      - gcic_static_data:/usr/src/app/RPS_project/staticfiles
      - gcic_media_data:/usr/src/app/RPS_project/media
      - ./logs:/var/log/nginx:ro
      - ./backend/RPS_project/feedback/gemma_embeddings_with_content.json:/usr/src/app/RPS_project/feedback/gemma_embeddings_with_content.json
    networks:
      - gcic-network
    restart: on-failure

  # Celery Worker
  celery:
    build: ./backend
    container_name: gcic-celery
    env_file:
      - .env
    command: >
      sh -c "
      echo 'Waiting for Redis...' &&
      until nc -z redis 6379; do sleep 2; done &&
      echo 'Waiting for Qdrant...' &&
      until nc -z qdrant 6333; do sleep 2; done &&
      echo 'All services are ready. Starting Celery...' &&
      celery -A RPS_project worker -l info
      "
    volumes:
      - ./backend:/usr/src/app
      - ./logs:/usr/src/app/logs:ro
    depends_on:
      - redis
      - gcic-backend
      - qdrant
    environment:
      DB_HOST: gcic-db
      DB_NAME: ${MYSQL_DATABASE}
      DB_USER: ${MYSQL_USER}
      DB_PASSWORD: ${MYSQL_PASSWORD}
      REDIS_HOST: redis
      REDIS_PORT: 6379
      QDRANT_URL: http://qdrant:6333
    networks:
      - gcic-network

  # Celery Beat Scheduler
  celery-beat:
    build: ./backend
    container_name: gcic-celery-beat
    env_file:
      - .env
    command: >
      sh -c "
      echo 'Waiting for Redis...' &&
      until nc -z redis 6379; do sleep 2; done &&
      echo 'Waiting for Qdrant...' &&
      until nc -z qdrant 6333; do sleep 2; done &&
      echo 'All services are ready. Starting Celery beat...' &&
      celery -A RPS_project beat -l info
      "
    depends_on:
      - redis
      - gcic-backend
      - qdrant
    environment:
      DB_HOST: gcic-db
      DB_NAME: ${MYSQL_DATABASE}
      DB_USER: ${MYSQL_USER}
      DB_PASSWORD: ${MYSQL_PASSWORD}
      REDIS_HOST: redis
      REDIS_PORT: 6379
      QDRANT_URL: http://qdrant:6333
    networks:
      - gcic-network

  # React build stage
  gcic-frontend-build:
    build:
      context: ./frontend
      target: builder
    container_name: gcic-react-image
    volumes:
      - gcic_react_build:/app/build
    networks:
      - gcic-network

  # Nginx proxy server
  gcic-nginx:
    image: nginx:alpine
    container_name: gcic-nginx
    ports:
      - "8791:80"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
      - gcic_static_data:/staticfiles:ro
      - gcic_media_data:/mediafiles:ro
      - gcic_react_build:/var/www/html:ro
      - ./logs:/var/log/nginx
    depends_on:
      - gcic-backend
      - gcic-frontend-build
    networks:
      - gcic-network
    restart: unless-stopped

  # Prometheus
  prometheus:
    image: prom/prometheus:latest
    container_name: gcic-prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
    command: --config.file=/etc/prometheus/prometheus.yml
    networks:
      - gcic-network
    depends_on:
      - gcic-backend

  # cAdvisor
  cadvisor:
    image: gcr.io/cadvisor/cadvisor:latest
    container_name: gcic-cadvisor
    ports:
      - "9080:8080"
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:ro
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:ro
      - /dev/disk/:/dev/disk:ro
    networks:
      - gcic-network

volumes:
  gcic_mysql_data:
  gcic_static_data:
  gcic_media_data:
  gcic_react_build:
  gcic_redis_data:

networks:
  gcic-network:
    driver: bridge
