version: '3.8'

services:
  # API Server
  api:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: fair-e-ship-api
    restart: unless-stopped
    ports:
      - "3000:3000"
      - "443:443"
    environment:
      - NODE_ENV=production
      - PORT=3000
      - HTTPS_PORT=443
      - USE_HTTPS=true
      - MONGODB_URI=mongodb://mongodb:27017/fair-e-ship
      - JWT_SECRET=${JWT_SECRET:-your-secret-key-change-this-in-production}
      - JWT_EXPIRES_IN=7d
      - RATE_LIMIT_WINDOW_MS=900000
      - RATE_LIMIT_MAX_REQUESTS=100
      - CORS_ORIGIN=*
    volumes:
      - ./logs:/app/logs
      - ./certs:/app/certs
    depends_on:
      - mongodb
    networks:
      - fair-e-ship-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 10s

  # MongoDB Database
  mongodb:
    image: mongo:6
    container_name: fair-e-ship-mongodb
    restart: unless-stopped
    environment:
      - MONGO_INITDB_DATABASE=fair-e-ship
    volumes:
      - mongodb-data:/data/db
    ports:
      - "27017:27017"
    networks:
      - fair-e-ship-network
    healthcheck:
      test: echo 'db.runCommand("ping").ok' | mongosh localhost:27017/fair-e-ship --quiet
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 10s

  # Nginx Reverse Proxy
  nginx:
    image: nginx:alpine
    container_name: fair-e-ship-nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "8443:443"
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./nginx/ssl:/etc/nginx/ssl
      - ./nginx/logs:/var/log/nginx
    depends_on:
      - api
    networks:
      - fair-e-ship-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 10s

  # Redis for caching and session storage
  redis:
    image: redis:alpine
    container_name: fair-e-ship-redis
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - redis-data:/data
    networks:
      - fair-e-ship-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 10s

  # Monitoring with Prometheus
  prometheus:
    image: prom/prometheus
    container_name: fair-e-ship-prometheus
    restart: unless-stopped
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus:/etc/prometheus
      - prometheus-data:/prometheus
    networks:
      - fair-e-ship-network
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/usr/share/prometheus/console_libraries'
      - '--web.console.templates=/usr/share/prometheus/consoles'

  # Visualization with Grafana
  grafana:
    image: grafana/grafana
    container_name: fair-e-ship-grafana
    restart: unless-stopped
    ports:
      - "3001:3000"
    volumes:
      - grafana-data:/var/lib/grafana
    depends_on:
      - prometheus
    networks:
      - fair-e-ship-network
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_ADMIN_PASSWORD:-admin}
      - GF_USERS_ALLOW_SIGN_UP=false

volumes:
  mongodb-data:
  redis-data:
  prometheus-data:
  grafana-data:

networks:
  fair-e-ship-network:
    driver: bridge