version: '3.8'

services:
  # MySQL 数据库
  mysql:
    image: mysql:8.0
    container_name: cloud-drive-mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: cloud_drive
      MYSQL_USER: clouduser
      MYSQL_PASSWORD: cloudpass
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql
      - ./backend/database/init:/docker-entrypoint-initdb.d
    networks:
      - cloud-drive-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      timeout: 20s
      retries: 10

  # Redis 缓存
  redis:
    image: redis:7-alpine
    container_name: cloud-drive-redis
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    networks:
      - cloud-drive-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      timeout: 10s
      retries: 5

  # FastAPI 后端
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: cloud-drive-backend
    restart: unless-stopped
    environment:
      - DATABASE_URL=mysql+pymysql://clouduser:cloudpass@mysql:3306/cloud_drive
      - REDIS_URL=redis://redis:6379
      - JWT_SECRET_KEY=your-super-secret-jwt-key-change-in-production
      - ENVIRONMENT=development
    ports:
      - "8000:8000"
    volumes:
      - ./backend:/app
      - backend_data:/app/uploads
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - cloud-drive-network
    command: uvicorn main:app --host 0.0.0.0 --port 8000 --reload

  # React 前端
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile.dev
    container_name: cloud-drive-frontend
    restart: unless-stopped
    environment:
      - VITE_API_BASE_URL=http://localhost:8080/api/v1
    ports:
      - "3000:3000"
    volumes:
      - ./frontend:/app
      - /app/node_modules
    depends_on:
      - backend
    networks:
      - cloud-drive-network
    command: npm run dev

volumes:
  mysql_data:
  redis_data:
  backend_data:

networks:
  cloud-drive-network:
    driver: bridge