#!/bin/bash

# 情绪博物馆 - 一键式部署脚本
# 同时部署后端、前端和管理后台到远程服务器 101.200.208.45
# 使用方法: bash deploy-all.sh [backend|frontend|admin|all]
# 默认部署所有服务

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 日志函数
log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

log_section() {
    echo ""
    echo -e "${BLUE}╔════════════════════════════════════════════════════════════════╗${NC}"
    echo -e "${BLUE}║${NC} $1"
    echo -e "${BLUE}╚════════════════════════════════════════════════════════════════╝${NC}"
    echo ""
}

# 获取脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

# 部署类型（默认为all）
DEPLOY_TYPE="${1:-all}"

# 验证部署类型
if [[ ! "$DEPLOY_TYPE" =~ ^(backend|frontend|admin|all)$ ]]; then
    log_error "无效的部署类型: $DEPLOY_TYPE"
    echo "使用方法: bash deploy-all.sh [backend|frontend|admin|all]"
    exit 1
fi

# 记录开始时间
START_TIME=$(date +%s)

# ============================================================================
# 部署后端
# ============================================================================
deploy_backend() {
    log_section "开始部署后端服务"
    
    if [ ! -f "backend-single/deploy.sh" ]; then
        log_error "后端部署脚本不存在: backend-single/deploy.sh"
        return 1
    fi
    
    log_info "执行后端部署脚本..."
    cd backend-single
    
    if bash deploy.sh remote; then
        log_info "✅ 后端部署成功"
        cd ..
        return 0
    else
        log_error "❌ 后端部署失败"
        cd ..
        return 1
    fi
}

# ============================================================================
# 部署前端
# ============================================================================
deploy_frontend() {
    log_section "开始部署前端应用"
    
    if [ ! -f "web/deploy.sh" ]; then
        log_error "前端部署脚本不存在: web/deploy.sh"
        return 1
    fi
    
    log_info "执行前端部署脚本..."
    cd web
    
    if bash deploy.sh; then
        log_info "✅ 前端部署成功"
        cd ..
        return 0
    else
        log_error "❌ 前端部署失败"
        cd ..
        return 1
    fi
}

# ============================================================================
# 部署管理后台
# ============================================================================
deploy_admin() {
    log_section "开始部署管理后台"
    
    if [ ! -f "web-admin/deploy.sh" ]; then
        log_error "管理后台部署脚本不存在: web-admin/deploy.sh"
        return 1
    fi
    
    log_info "执行管理后台部署脚本..."
    cd web-admin
    
    if bash deploy.sh; then
        log_info "✅ 管理后台部署成功"
        cd ..
        return 0
    else
        log_error "❌ 管理后台部署失败"
        cd ..
        return 1
    fi
}

# ============================================================================
# 主程序
# ============================================================================

log_section "情绪博物馆 - 一键式部署"
log_info "部署类型: $DEPLOY_TYPE"
log_info "部署时间: $(date '+%Y-%m-%d %H:%M:%S')"

BACKEND_SUCCESS=true
FRONTEND_SUCCESS=true
ADMIN_SUCCESS=true

# 执行部署
case "$DEPLOY_TYPE" in
    backend)
        if ! deploy_backend; then
            BACKEND_SUCCESS=false
        fi
        ;;
    frontend)
        if ! deploy_frontend; then
            FRONTEND_SUCCESS=false
        fi
        ;;
    admin)
        if ! deploy_admin; then
            ADMIN_SUCCESS=false
        fi
        ;;
    all)
        if ! deploy_backend; then
            BACKEND_SUCCESS=false
        fi
        
        if ! deploy_frontend; then
            FRONTEND_SUCCESS=false
        fi
        
        if ! deploy_admin; then
            ADMIN_SUCCESS=false
        fi
        ;;
esac

# ============================================================================
# 部署总结
# ============================================================================

END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))

log_section "部署完成总结"

if [ "$DEPLOY_TYPE" = "backend" ] || [ "$DEPLOY_TYPE" = "all" ]; then
    if [ "$BACKEND_SUCCESS" = true ]; then
        log_info "✅ 后端部署: 成功"
    else
        log_error "❌ 后端部署: 失败"
    fi
fi

if [ "$DEPLOY_TYPE" = "frontend" ] || [ "$DEPLOY_TYPE" = "all" ]; then
    if [ "$FRONTEND_SUCCESS" = true ]; then
        log_info "✅ 前端部署: 成功"
    else
        log_error "❌ 前端部署: 失败"
    fi
fi

if [ "$DEPLOY_TYPE" = "admin" ] || [ "$DEPLOY_TYPE" = "all" ]; then
    if [ "$ADMIN_SUCCESS" = true ]; then
        log_info "✅ 管理后台部署: 成功"
    else
        log_error "❌ 管理后台部署: 失败"
    fi
fi

log_info "部署耗时: ${DURATION}秒"

# ============================================================================
# 访问信息
# ============================================================================

# 检查部署结果
ALL_SUCCESS=true
if [ "$DEPLOY_TYPE" = "all" ]; then
    if [ "$BACKEND_SUCCESS" = false ] || [ "$FRONTEND_SUCCESS" = false ] || [ "$ADMIN_SUCCESS" = false ]; then
        ALL_SUCCESS=false
    fi
elif [ "$DEPLOY_TYPE" = "backend" ] && [ "$BACKEND_SUCCESS" = false ]; then
    ALL_SUCCESS=false
elif [ "$DEPLOY_TYPE" = "frontend" ] && [ "$FRONTEND_SUCCESS" = false ]; then
    ALL_SUCCESS=false
elif [ "$DEPLOY_TYPE" = "admin" ] && [ "$ADMIN_SUCCESS" = false ]; then
    ALL_SUCCESS=false
fi

if [ "$ALL_SUCCESS" = true ]; then
    echo ""
    log_section "部署成功！"
    
    if [ "$DEPLOY_TYPE" = "backend" ] || [ "$DEPLOY_TYPE" = "all" ]; then
        log_info "🔌 后端API地址: http://101.200.208.45:19089/api"
        log_info "📊 WebSocket地址: ws://101.200.208.45:19089/ws"
    fi
    
    if [ "$DEPLOY_TYPE" = "frontend" ] || [ "$DEPLOY_TYPE" = "all" ]; then
        log_info "📱 前端访问地址: http://101.200.208.45/emotion-museum/"
    fi
    
    if [ "$DEPLOY_TYPE" = "admin" ] || [ "$DEPLOY_TYPE" = "all" ]; then
        log_info "🔧 管理后台地址: http://101.200.208.45/emotion-museum-admin/"
    fi
    
    echo ""
    exit 0
else
    log_error "❌ 部署失败"
    exit 1
fi

