#!/bin/bash
# 环境快速切换脚本

ENV_TYPE=${1:-development}

echo "=== 环境切换工具 ==="
echo "目标环境: $ENV_TYPE"
echo ""

case $ENV_TYPE in
    dev|development)
        echo "🔄 切换到开发环境..."
        export NODE_ENV=development
        export ENV=development
        export VITE_APP_ENV=development
        
        # 停止可能运行的生产环境
        docker-compose down 2>/dev/null || true
        
        # 启动开发环境
        docker-compose -f docker-compose.dev.yml up -d
        
        echo "✅ 开发环境已启动"
        echo "前端地址: http://localhost:3001"
        echo "后端地址: http://localhost:8000"
        ;;
        
    prod|production)
        echo "🔄 切换到生产环境..."
        export NODE_ENV=production
        export ENV=production
        export VITE_APP_ENV=production
        
        # 停止开发环境
        docker-compose -f docker-compose.dev.yml down 2>/dev/null || true
        
        # 启动生产环境
        docker-compose up -d
        
        echo "✅ 生产环境已启动"
        echo "前端地址: http://localhost:3001"
        echo "后端地址: http://localhost:8000"
        ;;
        
    stop)
        echo "🛑 停止所有环境..."
        docker-compose -f docker-compose.dev.yml down 2>/dev/null || true
        docker-compose down 2>/dev/null || true
        echo "✅ 所有环境已停止"
        ;;
        
    status)
        echo "📊 当前环境状态:"
        ./scripts/check_environment.sh
        ;;
        
    *)
        echo "❌ 无效的环境类型: $ENV_TYPE"
        echo ""
        echo "用法: $0 [环境类型]"
        echo ""
        echo "环境类型:"
        echo "  dev, development  - 开发环境"
        echo "  prod, production  - 生产环境"
        echo "  stop              - 停止所有环境"
        echo "  status            - 查看环境状态"
        echo ""
        echo "示例:"
        echo "  $0 dev            # 切换到开发环境"
        echo "  $0 prod           # 切换到生产环境"
        echo "  $0 stop           # 停止所有环境"
        echo "  $0 status         # 查看状态"
        exit 1
        ;;
esac
