services:
  mysql:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-123456}
      MYSQL_DATABASE: ${MYSQL_DATABASE:-feedsystem}
      TZ: "Asia/Shanghai"
    ports:
      - "3307:3306"
    volumes:
      - mysql_data:/var/lib/mysql
    command:
      - --default-authentication-plugin=mysql_native_password
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_0900_ai_ci
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -uroot -p$${MYSQL_ROOT_PASSWORD} --silent"]
      interval: 5s
      timeout: 5s
      retries: 20

  redis:
    image: redis:7-alpine
    restart: always
    environment:
      REDIS_PASSWORD: ${REDIS_PASSWORD:-123456}
    command: ["redis-server", "--appendonly", "yes", "--requirepass", "${REDIS_PASSWORD:-123456}"]
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD-SHELL", "redis-cli -a \"$${REDIS_PASSWORD}\" ping"]
      interval: 5s
      timeout: 3s
      retries: 20

  rabbitmq:
    image: rabbitmq:3-management
    restart: always
    ports:
      - "5672:5672"
      - "15672:15672"
    environment:
      RABBITMQ_DEFAULT_USER: ${RABBITMQ_USER:-admin}
      RABBITMQ_DEFAULT_PASS: ${RABBITMQ_PASS:-password123}
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
    healthcheck:
      test: ["CMD-SHELL", "rabbitmq-diagnostics -q ping"]
      interval: 5s
      timeout: 5s
      retries: 20

  backend:
    build:
      context: .
      dockerfile: backend/Dockerfile
      target: api
    restart: always
    environment:
      JWT_SECRET: ${JWT_SECRET:-feedsystem-dev-secret-key}
      MYSQL_DATABASE: ${MYSQL_DATABASE:-feedsystem}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-123456}
      REDIS_PASSWORD: ${REDIS_PASSWORD:-123456}
      RABBITMQ_USER: ${RABBITMQ_USER:-admin}
      RABBITMQ_PASS: ${RABBITMQ_PASS:-password123}
    ports:
      - "8080:8080"
    volumes:
      - ./backend/configs/config.docker.yaml:/app/configs/config.yaml:ro
      - backend_uploads:/app/.run/uploads
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "pgrep api || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 3

  worker:
    build:
      context: .
      dockerfile: backend/Dockerfile
      target: worker
    restart: always
    environment:
      JWT_SECRET: ${JWT_SECRET:-feedsystem-dev-secret-key}
      MYSQL_DATABASE: ${MYSQL_DATABASE:-feedsystem}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-123456}
      REDIS_PASSWORD: ${REDIS_PASSWORD:-123456}
      RABBITMQ_USER: ${RABBITMQ_USER:-admin}
      RABBITMQ_PASS: ${RABBITMQ_PASS:-password123}
    volumes:
      - ./backend/configs/config.docker.yaml:/app/configs/config.yaml:ro
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "pgrep worker || exit 1"]
      interval: 15s
      timeout: 5s
      retries: 3

  frontend:
    build:
      context: .
      dockerfile: frontend/Dockerfile
    restart: always
    ports:
      - "5173:80"
    depends_on:
      - backend
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://localhost:80/ || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 3

volumes:
  mysql_data:
  redis_data:
  rabbitmq_data:
  backend_uploads:

