# ============================================
# BDW COOPERATIVE - DOCKER COMPOSE
# Production container orchestration
# ============================================

version: '3.8'

services:
  # MySQL Database
  mysql:
    image: mysql:8.0
    container_name: bdw-mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASSWORD}
    volumes:
      - mysql_data:/var/lib/mysql
      - ./database/schema.sql:/docker-entrypoint-initdb.d/01-schema.sql
    ports:
      - "3306:3306"
    networks:
      - bdw-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Node.js API
  api:
    build:
      context: ./server
      dockerfile: Dockerfile
    container_name: bdw-api
    restart: unless-stopped
    env_file:
      - ./server/.env.production
    environment:
      - NODE_ENV=production
      - DB_HOST=mysql
    depends_on:
      mysql:
        condition: service_healthy
    ports:
      - "3000:3000"
    networks:
      - bdw-network
    volumes:
      - ./server/logs:/app/logs
      - uploads:/app/uploads
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/api/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Background Worker
  worker:
    build:
      context: ./server
      dockerfile: Dockerfile.worker
    container_name: bdw-worker
    restart: unless-stopped
    env_file:
      - ./server/.env.production
    environment:
      - NODE_ENV=production
      - DB_HOST=mysql
    depends_on:
      mysql:
        condition: service_healthy
    networks:
      - bdw-network
    volumes:
      - ./server/logs:/app/logs

  # Nginx Reverse Proxy
  nginx:
    image: nginx:alpine
    container_name: bdw-nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./deployment/nginx.conf:/etc/nginx/conf.d/default.conf
      - ./client:/var/www/behinddwheel.com/client
      - ssl_certs:/etc/letsencrypt
    depends_on:
      - api
    networks:
      - bdw-network

volumes:
  mysql_data:
    driver: local
  uploads:
    driver: local
  ssl_certs:
    driver: local

networks:
  bdw-network:
    driver: bridge