version: '3.8'

services:
  db:
    build: ./db-init
    container_name: ppr_nextgen_db
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASSWORD}
    volumes:
      - mysql_data:/var/lib/mysql
    networks:
      - private_network

  # FastAPI Backend
  api:
    build: ./backend
    container_name: ppr_nextgen_api
    restart: unless-stopped
    environment:
      DB_HOST: ${DB_HOST}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}
      DB_NAME: ${DB_NAME}
      DB_PORT: ${DB_PORT}
      SECRET_KEY: ${SECRET_KEY}
      ACCESS_TOKEN_EXPIRE_MINUTES: ${ACCESS_TOKEN_EXPIRE_MINUTES}
      API_V1_STR: ${API_V1_STR}
      PROJECT_NAME: ${PROJECT_NAME}
      MAIL_HOST: ${MAIL_HOST}
      MAIL_PORT: ${MAIL_PORT}
      MAIL_USERNAME: ${MAIL_USERNAME}
      MAIL_PASSWORD: ${MAIL_PASSWORD}
      MAIL_FROM: ${MAIL_FROM}
      MAIL_FROM_NAME: ${MAIL_FROM_NAME}
      BASE_URL: ${BASE_URL}
      REDIS_URL: ${REDIS_URL}
      TOWER_NAME: ${TOWER_NAME}
      TOP_BAR_BASE_COLOR: ${TOP_BAR_BASE_COLOR}
      ENVIRONMENT: ${ENVIRONMENT}
    ports:
      - "${API_PORT_EXTERNAL}:8000"  # Use different port to avoid conflicts with existing system
    depends_on:
      - db
    volumes:
      - ./backend:/app
      - ./db-init:/db-init:ro  # Mount CSV data for seeding
      - ./web/assets:/web/assets  # Mount assets for QR code generation
    networks:
      - private_network
      - public_network

  # Nginx web server for public frontend
  web:
    image: nginx:alpine
    restart: unless-stopped
    environment:
      BASE_URL: ${BASE_URL}
    command: >
      sh -c "echo 'window.PPR_CONFIG = { apiBase: \"'\$BASE_URL'/api/v1\" };' > /usr/share/nginx/html/config.js &&
             nginx -g 'daemon off;'"
    ports:
      - "${WEB_PORT_EXTERNAL}:80"
    volumes:
      - ./web:/usr/share/nginx/html
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - api
    networks:
      - public_network

  # Redis for caching (optional for now)
  redis:
    image: redis:7-alpine
    container_name: ppr_nextgen_redis
    restart: unless-stopped
    networks:
      - private_network

  # phpMyAdmin for database management
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: ppr_nextgen_phpmyadmin
    restart: unless-stopped
    environment:
      PMA_HOST: ${PMA_HOST}
      PMA_PORT: ${DB_PORT}
      PMA_USER: ${DB_USER}
      PMA_PASSWORD: ${DB_PASSWORD}
      UPLOAD_LIMIT: ${UPLOAD_LIMIT}
    ports:
      - "${PMA_PORT_EXTERNAL}:80"  # phpMyAdmin web interface
    depends_on:
      - db
    networks:
      - private_network
      - public_network

volumes:
  mysql_data:

networks:
  private_network:
    driver: bridge
  public_network:
    driver: bridge