#!/bin/bash
# ============================================================
# 一键部署前端+后端到 dm.all8ai.top 服务器
# 用法:
#   bash deploy.sh          # 全量部署（后端+前端）
#   bash deploy.sh server   # 只部署后端
#   bash deploy.sh front    # 只部署前端
# ============================================================

set -e

# 服务器配置
SERVER_IP="152.136.182.184"
SERVER_USER="ubuntu"
REMOTE_WEB="/var/www/dm.all8ai.top"
REMOTE_API="/opt/sbrain-server"

# 项目路径
PROJECT_DIR="$(cd "$(dirname "$0")" && pwd)"
CLIENT_DIR="$PROJECT_DIR/client"
SERVER_DIR="$PROJECT_DIR/server"

TARGET="${1:-all}"

# ============================================================
# 部署后端
# ============================================================
deploy_server() {
  echo "=========================================="
  echo "  部署后端到 dm.all8ai.top"
  echo "=========================================="
  echo ""

  # 1. 同步代码
  echo "[1/4] 同步代码..."
  rsync -avz --delete \
    --exclude='node_modules' \
    --exclude='.env' \
    --exclude='dist' \
    --exclude='.git' \
    "$SERVER_DIR/" "$SERVER_USER@$SERVER_IP:$REMOTE_API/"
  echo "✓ 代码同步完成"
  echo ""

  # 2. 安装依赖
  echo "[2/4] 安装依赖..."
  ssh "$SERVER_USER@$SERVER_IP" "cd $REMOTE_API && npm install --production"
  echo "✓ 依赖安装完成"
  echo ""

  # 3. 写入服务器 .env
  echo "[3/4] 写入服务器配置..."
  ssh "$SERVER_USER@$SERVER_IP" "cat > $REMOTE_API/.env << 'ENVEOF'
DB_HOST=127.0.0.1
DB_PORT=15432
DB_NAME=bill_query
DB_USER=freedak
DB_PASSWORD=
DB_POOL_MAX=10

PORT=9333
JWT_SECRET=malan-ops-2026-secret-key
CLIENT_URL=https://dm.all8ai.top
ZHIPU_API_KEY=sk-c0c5174892c44ff48d587cd040fbdd40

ADMIN_DB_HOST=127.0.0.1
ADMIN_DB_PORT=5432
ADMIN_DB_NAME=sbrain_admin
ADMIN_DB_USER=sbrain_admin
ADMIN_DB_PASSWORD=sbrain2026
TTS_API_KEY=sk-ws-H.EHYXPRE.Gv4a.MEQCIDOofoXP-VBQILl0c_kspJZhSUj8fyYIZupigveHnpRrAiAsjWXuJ3dpyTBDCUF1ZLdcvLz8GJoan0ObIkCwfE06kQ
TTS_API_HOST=llm-znfsxsdp6uwik7ad.cn-beijing.maas.aliyuncs.com
ENVEOF"
  echo "✓ 配置写入完成"
  echo ""

  # 4. pm2 重启
  echo "[4/4] 重启后端服务..."
  ssh "$SERVER_USER@$SERVER_IP" "cd $REMOTE_API && pm2 delete sbrain-server 2>/dev/null; pm2 start 'npx tsx src/index.ts' --name sbrain-server && pm2 save"
  echo "✓ 后端已启动"
  echo ""

  echo "=========================================="
  echo "  后端部署成功！端口 9333"
  echo "  日志: ssh $SERVER_USER@$SERVER_IP 'pm2 logs sbrain-server'"
  echo "=========================================="
  echo ""
}

# ============================================================
# 部署前端
# ============================================================
deploy_front() {
  echo "=========================================="
  echo "  部署前端到 dm.all8ai.top"
  echo "=========================================="
  echo ""

  # 1. 构建前端
  echo "[1/3] 构建前端..."
  cd "$CLIENT_DIR"
  npm run build
  echo "✓ 构建完成"
  echo ""

  # 2. 清理服务器旧文件
  echo "[2/3] 清理服务器旧文件..."
  ssh "$SERVER_USER@$SERVER_IP" "rm -rf $REMOTE_WEB/assets"
  echo "✓ 旧文件已清理"
  echo ""

  # 3. 上传构建产物
  echo "[3/3] 上传构建产物..."
  scp -r "$CLIENT_DIR/dist/"* "$SERVER_USER@$SERVER_IP:$REMOTE_WEB/"
  echo "✓ 上传完成"
  echo ""

  echo "=========================================="
  echo "  前端部署成功！"
  echo "  访问: https://dm.all8ai.top"
  echo "=========================================="
  echo ""
}

# ============================================================
# 执行
# ============================================================
case "$TARGET" in
  server)
    deploy_server
    ;;
  front)
    deploy_front
    ;;
  all)
    deploy_server
    deploy_front
    ;;
  *)
    echo "用法: bash deploy.sh [all|server|front]"
    echo "  all    - 部署后端+前端（默认）"
    echo "  server - 只部署后端"
    echo "  front  - 只部署前端"
    exit 1
    ;;
esac
