#!/bin/bash
# =============================================================================
# 智能IT支持服务台 — 生产部署脚本
# 目标服务器：10.90.5.110
# 部署路径：/opt/wecom-it-desk
# =============================================================================

set -e  # 遇到错误立即停止

DEPLOY_DIR="/opt/wecom-it-desk"
BACKUP_DIR="/opt/wecom-it-desk-backup-$(date +%Y%m%d_%H%M%S)"

echo "=========================================="
echo "智能IT支持服务台 生产部署"
echo "时间: $(date)"
echo "=========================================="

# --------------------------------------------------------------------------
# 1. 备份当前版本
# --------------------------------------------------------------------------
echo ""
echo ">>> 步骤1: 备份当前版本..."
mkdir -p "$BACKUP_DIR"
cp -r "$DEPLOY_DIR/frontend-h5/dist" "$BACKUP_DIR/frontend-h5-dist" 2>/dev/null || true
cp -r "$DEPLOY_DIR/frontend-agent/dist" "$BACKUP_DIR/frontend-agent-dist" 2>/dev/null || true
cp -r "$DEPLOY_DIR/frontend-admin/dist" "$BACKUP_DIR/frontend-admin-dist" 2>/dev/null || true
cp -r "$DEPLOY_DIR/frontend-portal/dist" "$BACKUP_DIR/frontend-portal-dist" 2>/dev/null || true
cp -r "$DEPLOY_DIR/backend" "$BACKUP_DIR/backend" 2>/dev/null || true
echo "备份完成: $BACKUP_DIR"

# --------------------------------------------------------------------------
# 2. 解压前端文件
# --------------------------------------------------------------------------
echo ""
echo ">>> 步骤2: 解压前端文件..."
cd "$DEPLOY_DIR"

# H5 前端
rm -rf frontend-h5/dist
tar -xf /tmp/deploy-h5.tar -C frontend-h5/
echo "H5 前端已更新"

# 坐席前端
rm -rf frontend-agent/dist
tar -xf /tmp/deploy-agent.tar -C frontend-agent/
echo "坐席前端已更新"

# 管理后台
rm -rf frontend-admin/dist
tar -xf /tmp/deploy-admin.tar -C frontend-admin/
echo "管理后台已更新"

# Portal 统一入口
rm -rf frontend-portal/dist
tar -xf /tmp/deploy-portal.tar -C frontend-portal/ 2>/dev/null || echo "Portal 包未找到，跳过"
echo "Portal 已更新"

# --------------------------------------------------------------------------
# 3. 更新后端代码
# --------------------------------------------------------------------------
echo ""
echo ">>> 步骤3: 更新后端代码..."
cd "$DEPLOY_DIR"

# 备份后端 .env（生产配置，不能覆盖）
cp backend/.env /tmp/backend-env-backup 2>/dev/null || true

# 解压后端代码（覆盖旧文件）
tar -xf /tmp/deploy-backend.tar -C ./

# 恢复后端 .env
cp /tmp/backend-env-backup backend/.env 2>/dev/null || true
echo "后端代码已更新"

# --------------------------------------------------------------------------
# 4. 数据库迁移（角色系统）
# --------------------------------------------------------------------------
echo ""
echo ">>> 步骤4: 执行数据库迁移..."
cd "$DEPLOY_DIR/backend"
alembic upgrade head
echo "数据库迁移完成"

# --------------------------------------------------------------------------
# 5. 关闭 Mock 登录（如果还开着）
# --------------------------------------------------------------------------
echo ""
echo ">>> 步骤5: 检查 Mock 登录配置..."
if grep -q "MOCK_LOGIN_ENABLED=true" "$DEPLOY_DIR/backend/.env" 2>/dev/null || \
   grep -q "MOCK_LOGIN_ENABLED=true" "$DEPLOY_DIR/.env" 2>/dev/null; then
    echo "发现 MOCK_LOGIN_ENABLED=true，正在关闭..."
    sed -i 's/MOCK_LOGIN_ENABLED=true/MOCK_LOGIN_ENABLED=false/g' "$DEPLOY_DIR/backend/.env" 2>/dev/null || true
    sed -i 's/MOCK_LOGIN_ENABLED=true/MOCK_LOGIN_ENABLED=false/g' "$DEPLOY_DIR/.env" 2>/dev/null || true
    echo "Mock 登录已关闭"
else
    echo "Mock 登录已关闭，无需修改"
fi

# --------------------------------------------------------------------------
# 6. 重建后端 Docker 镜像
# --------------------------------------------------------------------------
echo ""
echo ">>> 步骤6: 重建后端 Docker 镜像..."
cd "$DEPLOY_DIR"
docker compose build --no-cache backend
echo "后端镜像重建完成"

# --------------------------------------------------------------------------
# 7. 重启所有容器
# --------------------------------------------------------------------------
echo ""
echo ">>> 步骤7: 重启所有容器..."
docker compose down
docker compose up -d
echo "容器重启完成"

# --------------------------------------------------------------------------
# 8. 等待服务就绪
# --------------------------------------------------------------------------
echo ""
echo ">>> 步骤8: 等待服务就绪..."
sleep 10

# 检查容器状态
echo ""
echo "容器状态:"
docker compose ps

# 检查后端健康
echo ""
echo "后端健康检查:"
curl -s http://localhost:8000/health || echo "后端未就绪，请稍后重试"

# 检查前端
echo ""
echo "前端健康检查:"
curl -s http://localhost:80/itdesk/health || echo "前端未就绪，请稍后重试"

echo ""
echo "=========================================="
echo "部署完成！"
echo "=========================================="
echo ""
echo "访问地址:"
echo "  统一入口: https://itsupport.servyou.com.cn/itportal/"
echo "  H5 用户端: https://itsupport.servyou.com.cn/itdesk/"
echo "  坐席工作台: https://itsupport.servyou.com.cn/itagent/"
echo "  管理后台: https://itsupport.servyou.com.cn/itadmin/"
echo ""
echo "如需回滚，执行:"
echo "  cp -r $BACKUP_DIR/frontend-h5-dist $DEPLOY_DIR/frontend-h5/dist"
echo "  cp -r $BACKUP_DIR/frontend-agent-dist $DEPLOY_DIR/frontend-agent/dist"
echo "  cp -r $BACKUP_DIR/frontend-admin-dist $DEPLOY_DIR/frontend-admin/dist"
echo "  cp -r $BACKUP_DIR/backend $DEPLOY_DIR/backend"
echo "  docker compose restart"
