#!/bin/bash
# =============================================================================
# 企微智能IT支持服务台 — 部署包生成脚本（在开发机上运行）
# =============================================================================
# 功能：
#   1. 构建前端（H5 + 坐席端）
#   2. 将前端产物 + 后端源码 + 配置文件打包为部署包
#   3. 输出 zip 文件，可上传到服务器直接解压部署
#
# 用法：
#   cd 项目根目录
#   bash deploy-server/package.sh
# =============================================================================

set -e

# 颜色输出
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

# 项目根目录（脚本在 deploy-server/ 下，需要回到上一级）
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
cd "$PROJECT_ROOT"

PACKAGE_NAME="it-smart-desk-server-deploy"
BUILD_DIR="/tmp/$PACKAGE_NAME"

echo -e "${GREEN}============================================${NC}"
echo -e "${GREEN}  智能IT支持服务台 — 部署包生成${NC}"
echo -e "${GREEN}============================================${NC}"

# --- 1. 构建前端 ---
echo ""
echo "--- [1/3] 构建前端 ---"

# H5 员工端
echo "构建 H5 员工端..."
cd "$PROJECT_ROOT/frontend-h5"
npm install --quiet
npm run build
echo -e "${GREEN}✓ H5 员工端构建完成${NC}"

# 坐席工作台
echo "构建坐席工作台..."
cd "$PROJECT_ROOT/frontend-agent"
npm install --quiet
npm run build
echo -e "${GREEN}✓ 坐席工作台构建完成${NC}"

cd "$PROJECT_ROOT"

# --- 2. 组装部署包 ---
echo ""
echo "--- [2/3] 组装部署包 ---"

# 清理旧目录
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR"

# 复制 Docker Compose 配置
cp -r "$SCRIPT_DIR/docker-compose.yml" "$BUILD_DIR/"
cp -r "$SCRIPT_DIR/.env.example" "$BUILD_DIR/"
cp -r "$SCRIPT_DIR/deploy.sh" "$BUILD_DIR/"
cp -r "$SCRIPT_DIR/README.md" "$BUILD_DIR/"

# 复制 Nginx 配置
mkdir -p "$BUILD_DIR/nginx"
cp "$SCRIPT_DIR/nginx/nginx.conf" "$BUILD_DIR/nginx/"

# 复制前端构建产物
mkdir -p "$BUILD_DIR/frontend-h5/dist"
cp -r "$PROJECT_ROOT/frontend-h5/dist/"* "$BUILD_DIR/frontend-h5/dist/"

mkdir -p "$BUILD_DIR/frontend-agent/dist"
cp -r "$PROJECT_ROOT/frontend-agent/dist/"* "$BUILD_DIR/frontend-agent/dist/"

# 复制后端源码
mkdir -p "$BUILD_DIR/backend"
cp -r "$PROJECT_ROOT/backend/"* "$BUILD_DIR/backend/"

# 清理后端中的缓存和临时文件
rm -rf "$BUILD_DIR/backend/__pycache__"
rm -rf "$BUILD_DIR/backend/app/__pycache__"
find "$BUILD_DIR/backend" -name "__pycache__" -type d -exec rm -rf {} + 2>/dev/null || true
find "$BUILD_DIR/backend" -name "*.pyc" -delete 2>/dev/null || true

echo -e "${GREEN}✓ 部署包组装完成${NC}"

# --- 3. 打包 ---
echo ""
echo "--- [3/3] 打包 ---"

cd /tmp
zip -r "$PROJECT_ROOT/${PACKAGE_NAME}.zip" "$PACKAGE_NAME" -q
cd "$PROJECT_ROOT"

# 清理临时目录
rm -rf "$BUILD_DIR"

# 输出信息
ZIP_SIZE=$(du -h "${PACKAGE_NAME}.zip" | cut -f1)
echo ""
echo -e "${GREEN}============================================${NC}"
echo -e "${GREEN}  部署包生成完成！${NC}"
echo -e "${GREEN}============================================${NC}"
echo ""
echo "文件：${PACKAGE_NAME}.zip (${ZIP_SIZE})"
echo ""
echo "部署步骤："
echo "  1. 上传到服务器: scp ${PACKAGE_NAME}.zip user@服务器IP:/opt/"
echo "  2. 解压: cd /opt && unzip ${PACKAGE_NAME}.zip && mv ${PACKAGE_NAME} wecom-it-desk"
echo "  3. 配置: cp .env.example .env && vi .env"
echo "  4. 部署: chmod +x deploy.sh && ./deploy.sh"
