#!/bin/bash
set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
CONFIG_FILE="$SCRIPT_DIR/.deploy-config"

if [ ! -f "$CONFIG_FILE" ]; then
  echo "❌ 缺少配置文件 .deploy-config"
  exit 1
fi

source "$CONFIG_FILE"

if ! command -v sshpass &> /dev/null; then
  echo "❌ 未安装 sshpass，请先运行: brew install hudochenkov/sshpass/sshpass"
  exit 1
fi

PASS_FILE=$(mktemp)
echo "$SERVER_PASS" > "$PASS_FILE"
trap "rm -f $PASS_FILE /tmp/nanami-web-deploy.tar.gz" EXIT

SSH_OPTS="-o StrictHostKeyChecking=no"

run_ssh() {
  sshpass -f "$PASS_FILE" ssh $SSH_OPTS "${SERVER_USER}@${SERVER_HOST}" "source /etc/profile 2>/dev/null; source ~/.bashrc 2>/dev/null; source ~/.bash_profile 2>/dev/null; $*"
}

run_scp() {
  sshpass -f "$PASS_FILE" scp $SSH_OPTS "$@"
}

echo ""
echo "========================================="
echo "  部署到 ${SERVER_HOST}"
echo "========================================="
echo ""

echo "=> [1/6] 本地安装依赖..."
cd "$SCRIPT_DIR"
npm install --silent

echo ""
echo "=> [2/6] 本地同步 Prisma..."
npx prisma generate

echo ""
echo "=> [3/6] 本地构建项目..."
npm run build

echo ""
echo "=> [4/6] 打包构建产物..."
TAR_FILE="/tmp/nanami-web-deploy.tar.gz"
STAGE_DIR="/tmp/nanami-web-stage"
rm -rf "$STAGE_DIR"

STANDALONE_ROOT="$SCRIPT_DIR/.next/standalone/gitLib/nanami-web"
if [ ! -f "$STANDALONE_ROOT/server.js" ]; then
  STANDALONE_ROOT="$SCRIPT_DIR/.next/standalone"
fi

mkdir -p "$STAGE_DIR/.next/standalone"
cp -r "$STANDALONE_ROOT/"* "$STAGE_DIR/.next/standalone/"
cp -r "$STANDALONE_ROOT/.next" "$STAGE_DIR/.next/standalone/"
mkdir -p "$STAGE_DIR/.next/standalone/.next/static"
cp -r "$SCRIPT_DIR/.next/static/"* "$STAGE_DIR/.next/standalone/.next/static/"
cp -r "$SCRIPT_DIR/public" "$STAGE_DIR/.next/standalone/"

cp -r "$SCRIPT_DIR/prisma" "$STAGE_DIR/"
cp "$SCRIPT_DIR/package.json" "$STAGE_DIR/"
cp "$SCRIPT_DIR/package-lock.json" "$STAGE_DIR/"
cp "$SCRIPT_DIR/ecosystem.config.js" "$STAGE_DIR/"

tar -czf "$TAR_FILE" -C "$STAGE_DIR" .
rm -rf "$STAGE_DIR"

echo "   打包完成: $(du -h "$TAR_FILE" | cut -f1)"

echo ""
echo "=> [5/6] 上传到服务器..."
run_ssh "mkdir -p ${REMOTE_DIR} && rm -rf ${REMOTE_DIR}/.next"
run_scp "$TAR_FILE" "${SERVER_USER}@${SERVER_HOST}:/tmp/nanami-web-deploy.tar.gz"
run_ssh "cd ${REMOTE_DIR} && tar -xzf /tmp/nanami-web-deploy.tar.gz && rm -f /tmp/nanami-web-deploy.tar.gz"

echo ""
echo "=> [6/6] 服务器安装生产依赖 & 同步数据库 & 重启..."
run_ssh "cd ${REMOTE_DIR} && \
  npm install --omit=dev --registry=https://registry.npmjs.org && \
  npx prisma generate && \
  npx prisma db push && \
  mkdir -p uploads && \
  ln -sf ${REMOTE_DIR}/uploads ${REMOTE_DIR}/.next/standalone/uploads && \
  ln -sf ${REMOTE_DIR}/.env ${REMOTE_DIR}/.next/standalone/.env && \
  ln -sf ${REMOTE_DIR}/src/generated ${REMOTE_DIR}/.next/standalone/src/generated 2>/dev/null; \
  cd ${REMOTE_DIR} && (pm2 delete nanami-web 2>/dev/null; pm2 start ecosystem.config.js)"

echo ""
echo "========================================="
echo "  ✅ 部署完成！"
echo "========================================="
echo ""
echo "网站地址: http://${SERVER_HOST}:3000"
echo "管理后台: http://${SERVER_HOST}:3000/admin/login"
echo ""
