version: '3.8'

services:
  mysql:
    image: mysql:8.0.36
    container_name: mci-mysql
    profiles: ["infra"]
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-root}
      MYSQL_DATABASE: ${MYSQL_DATABASE:-sino_mci}
    ports:
      - "${MYSQL_PORT:-3306}:3306"
    volumes:
      - mci-mysql-data:/var/lib/mysql
    networks:
      - mci-network
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h localhost -u root -p\"$${MYSQL_ROOT_PASSWORD}\" || exit 1"]
      interval: 5s
      timeout: 5s
      retries: 20
      start_period: 30s

  redis:
    image: redis:7.2
    container_name: mci-redis
    profiles: ["infra"]
    restart: unless-stopped
    ports:
      - "${REDIS_PORT:-6379}:6379"
    networks:
      - mci-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 10
      start_period: 10s

  rabbitmq:
    image: rabbitmq:3.13-management
    container_name: mci-rabbitmq
    profiles: ["infra"]
    restart: unless-stopped
    environment:
      RABBITMQ_DEFAULT_USER: ${RABBITMQ_DEFAULT_USER:-guest}
      RABBITMQ_DEFAULT_PASS: ${RABBITMQ_DEFAULT_PASS:-guest}
    ports:
      - "${RABBITMQ_AMQP_PORT:-5672}:5672"
      - "${RABBITMQ_MGMT_PORT:-15672}:15672"
    networks:
      - mci-network
    healthcheck:
      test: ["CMD-SHELL", "rabbitmq-diagnostics ping || exit 1"]
      interval: 5s
      timeout: 5s
      retries: 15
      start_period: 30s

  seaweedfs:
    image: chrislusf/seaweedfs:3.62
    container_name: mci-seaweedfs
    profiles: ["infra"]
    restart: unless-stopped
    ports:
      - "${SEAWEEDFS_MASTER_PORT:-9333}:9333"
      - "${SEAWEEDFS_S3_PORT:-8888}:8888"
    command: server -dir=/data
    volumes:
      - mci-seaweedfs-data:/data
    networks:
      - mci-network
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://localhost:9333/cluster/status >/dev/null 2>&1 || exit 1"]
      interval: 15s
      timeout: 10s
      retries: 8
      start_period: 60s

  mci-server:
    build:
      context: ./backend/mci-server
      dockerfile: Dockerfile
    container_name: mci-server
    profiles: ["app"]
    restart: unless-stopped
    ports:
      - "${MCI_SERVER_PORT:-8080}:8080"
    environment:
      - SPRING_PROFILES_ACTIVE=${SPRING_PROFILES_ACTIVE:-dev}
      - MYSQL_HOST=${MYSQL_HOST:-mci-mysql}
      - MYSQL_PORT=${MYSQL_PORT:-3306}
      - MYSQL_DATABASE=${MYSQL_DATABASE:-sino_mci}
      - MYSQL_USER=${MYSQL_USER:-root}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-root}
      - REDIS_HOST=${REDIS_HOST:-mci-redis}
      - REDIS_PORT=${REDIS_PORT:-6379}
      - RABBITMQ_HOST=${RABBITMQ_HOST:-mci-rabbitmq}
      - RABBITMQ_AMQP_PORT=${RABBITMQ_AMQP_PORT:-5672}
      - RABBITMQ_DEFAULT_USER=${RABBITMQ_DEFAULT_USER:-guest}
      - RABBITMQ_DEFAULT_PASS=${RABBITMQ_DEFAULT_PASS:-guest}
      - JAVA_OPTS=${JAVA_OPTS:--XX:+UseContainerSupport -XX:MaxRAMPercentage=75.0}
      - OPENAI_API_KEY=${OPENAI_API_KEY:-}
    networks:
      - mci-network
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-fsS", "http://localhost:8080/msg-platform/api/v1/health"]
      interval: 10s
      timeout: 5s
      retries: 15
      start_period: 90s

  admin-web:
    build:
      context: ./admin-web
      dockerfile: Dockerfile
    container_name: mci-admin-web
    profiles: ["app"]
    restart: unless-stopped
    ports:
      - "${ADMIN_WEB_PORT:-80}:80"
    networks:
      - mci-network
    depends_on:
      mci-server:
        condition: service_healthy

  channel-service:
    build:
      context: ./channel-service
      dockerfile: Dockerfile
    container_name: mci-channel-service
    profiles: ["app"]
    restart: unless-stopped
    ports:
      - "${CHANNEL_SERVICE_PORT:-8000}:8000"
    environment:
      - MCI_SERVER_URL=${MCI_SERVER_URL:-http://mci-server:8080/msg-platform}
      - EVENT_STORE_PATH=/app/data/events.db
      - MAX_RETRY=${MAX_RETRY:-5}
      - BASE_RETRY_DELAY_SECONDS=${BASE_RETRY_DELAY_SECONDS:-5}
      - EVENT_RETENTION_DAYS=${EVENT_RETENTION_DAYS:-10}
      - EVENT_PUBLISH_INTERVAL_SECONDS=${EVENT_PUBLISH_INTERVAL_SECONDS:-5}
      - OPENAI_API_KEY=${OPENAI_API_KEY:-}
    volumes:
      - channel-service-data:/app/data
    networks:
      - mci-network
    depends_on:
      mci-server:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-fsS", "http://localhost:8000/health"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 30s

volumes:
  mci-mysql-data:
  mci-seaweedfs-data:
  channel-service-data:

networks:
  mci-network:
    driver: bridge
