#!/bin/bash
# 超简部署：直接SSH到服务器执行 git pull → build → reload
# 用法: bash deploy-simple.sh
# 前提: 代码已 push 到 git.rrbrr.com

set -e

HOST="root@8.159.134.106"
PORT="22622"

ssh -p $PORT $HOST << 'ENDSSH'
set -e
cd /data/ai/audio_codebuddy

echo "=== 1. 拉取代码 ==="
git pull origin master
echo "当前提交: $(git log --oneline -1)"

echo ""
echo "=== 2. 构建后端 ==="
cd server

# 先生成 Prisma Client（schema 变更时必须）
npx prisma generate

# 清除旧构建，防止用旧 dist 跑（构建失败不 reload）
rm -rf dist
npm run build

echo ""
echo "=== 3. 复制 models.json ==="
mkdir -p dist/config && cp src/config/models.json dist/config/models.json 2>/dev/null || true

echo ""
echo "=== 4. 重启服务 ==="
pm2 reload server

echo ""
echo "=== 部署完成 ==="
pm2 status | grep server
ENDSSH

echo ""
echo "验证:"
curl -s -o /dev/null -w "HTTP %{http_code}" http://book.rrbrr.com/api/health 2>/dev/null && echo "" || echo " 连接失败"
