version: '3.8'

services:
  mongodb:
    image: mongo:7.0
    container_name: mlm-mongodb
    restart: unless-stopped
    ports:
      - "127.0.0.1:27017:27017"
    volumes:
      - mongodb_data:/data/db
    environment:
      - MONGO_INITDB_DATABASE=mlm_production
    networks:
      - mlm-network
    healthcheck:
      test: echo 'db.runCommand("ping").ok' | mongosh localhost:27017/mlm_production --quiet
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    container_name: mlm-redis
    restart: unless-stopped
    command: redis-server --appendonly yes
    ports:
      - "127.0.0.1:6379:6379"
    volumes:
      - redis_data:/data
    networks:
      - mlm-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 3

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: mlm-backend
    restart: unless-stopped
    ports:
      - "127.0.0.1:8001:8001"
    environment:
      - MONGO_URL=mongodb://mongodb:27017
      - DB_NAME=mlm_production
      - REDIS_URL=redis://redis:6379
      - HOST=0.0.0.0
      - PORT=8001
    depends_on:
      mongodb:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - mlm-network
    volumes:
      - ./backend/logs:/app/logs
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8001/api/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: mlm-frontend
    restart: unless-stopped
    ports:
      - "127.0.0.1:3000:80"
    environment:
      - REACT_APP_BACKEND_URL=http://backend:8001
      - REACT_APP_API_URL=http://backend:8001/api
    depends_on:
      - backend
    networks:
      - mlm-network

  nginx:
    image: nginx:alpine
    container_name: mlm-nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
    depends_on:
      - backend
      - frontend
    networks:
      - mlm-network
    healthcheck:
      test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3

volumes:
  mongodb_data:
    driver: local
  redis_data:
    driver: local

networks:
  mlm-network:
    driver: bridge
