version: '3.8'

services:
  # Database service (unchanged)
  db:
    image: mysql:8.0
    container_name: mysql-db
    environment:
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
    volumes:
      - mysql_data:/var/lib/mysql
    networks:
      - jitbot-network

  # Django backend - no direct port exposure
  backend:
    build: ./jitbot-backend
    container_name: django-backend
    env_file:
      - .env
    command: >
      sh -c "python manage.py makemigrations &&
             python manage.py migrate &&
             python manage.py collectstatic --noinput &&
             gunicorn --bind 0.0.0.0:8786 --workers 4 --timeout 90 --access-logfile /var/log/gunicorn-access.log --error-logfile /var/log/gunicorn-error.log chatbot_project.wsgi:application"
    depends_on:
      - db
    environment:
      - DB_HOST=db
      - DB_NAME=${MYSQL_DATABASE}
      - DB_USER=${MYSQL_USER}
      - DB_PASSWORD=${MYSQL_PASSWORD}
    volumes:
      - static_data:/usr/src/app/chatbot_project/staticfiles
      - media_data:/usr/src/app/chatbot_project/media
      - ./logs:/var/log
    networks:
      - jitbot-network

  # React build stage (no server, just build files)
  frontend-build:
    build: 
      context: ./jitbot-ui
      target: builder  # Stop at build stage, don't run nginx
    container_name: react-builder
    volumes:
      - react_build:/app/build
    networks:
      - jitbot-network

  # Main nginx proxy server (this replaces your frontend nginx)
  nginx:
    image: nginx:alpine
    container_name: jitbot-nginx
    ports:
      - "8786:80"  # Use the port your team leader suggested
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
      - static_data:/staticfiles:ro
      - media_data:/mediafiles:ro
      - react_build:/var/www/html:ro
    depends_on:
      - backend
      - frontend-build
    networks:
      - jitbot-network
    restart: unless-stopped

volumes:
  mysql_data:
  static_data:
  media_data:
  react_build:

networks:
  jitbot-network:
    driver: bridge