version: '3.8'

services:
  # Database service for RPS
  french-db:
    image: mysql:8.0
    container_name: french-mysql-database
    environment:
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
    volumes:
      - french_mysql_data:/var/lib/mysql
    networks:
      - french-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${MYSQL_ROOT_PASSWORD}"]
      interval: 10s
      timeout: 10s
      retries: 10
      start_period: 30s
      
  #REDIS 
  redis:
    image: redis:7-alpine
    container_name: french-redis
    ports:
      - "6379:6379"
    volumes:
      - french_redis_data:/data
    networks:
      - french-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped
    command: redis-server --appendonly yes

  qdrant:
    image: qdrant/qdrant:v1.12.3
    container_name: qdrant-db
    ports:
      - "6333:6333"
    volumes:
      - ./data/qdrant_storage:/qdrant/storage
    networks:
      - french-network
    restart: unless-stopped

  # Django backend
  french-backend:
    build:
      context: ./backend
      cache_from:
        - french-backend:latest
        - french-backend:previous
    image: french-backend:latest
    container_name: french-backend-image
    env_file:
      - .env
    command: >
      sh -c "
      echo 'Waiting for MySQL...' &&
      until nc -z french-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 8792 RPS_project.asgi:application
      "
    depends_on:
      - french-db
      - redis
      - qdrant
    environment:
      - DB_HOST=french-db
      - DB_NAME=${MYSQL_DATABASE}
      - DB_USER=${MYSQL_USER}
      - DB_PASSWORD=${MYSQL_PASSWORD}
      - REDIS_HOST=redis
      - REDIS_PORT=6379
    volumes:
      - french_static_data:/usr/src/app/RPS_project/staticfiles
      - french_media_data:/usr/src/app/RPS_project/media
      - ./logs:/var/log
      - ./backend/RPS_project/feedback/gemma_embeddings_with_content.json:/usr/src/app/RPS_project/feedback/gemma_embeddings_with_content.json
    networks:
      - french-network
    restart: on-failure

  # React build
  french-frontend-build:
    build:
      context: ./client-side
      target: builder
      cache_from:
        - french-frontend-build:latest
        - french-frontend-build:previous
    image: french-frontend-build:latest
    container_name: french-react-image
    volumes:
      - french_react_build:/app/dist
    networks:
      - french-network

  # Nginx proxy config
  french-nginx:
    image: nginx:alpine
    container_name: french-nginx
    ports:
      - "8792:80"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
      - french_static_data:/staticfiles:ro
      - french_media_data:/mediafiles:ro
      - french_react_build:/var/www/html:ro
    depends_on:
      - french-backend
      - french-frontend-build
    networks:
      - french-network
    restart: unless-stopped

# 🧱 Global volume declarations (MUST be at root level)
volumes:
  french_mysql_data:
  french_static_data:
  french_media_data:
  french_react_build:
  french_redis_data:

networks:
  french-network:
    driver: bridge