#!/usr/bin/env bash
# 构建 Cloudflare Pages 客户端 zip（mac / linux / win 分包）
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
RELEASE_DIR="$ROOT/client/download/releases"
BASE_STAGE="$RELEASE_DIR/.stage-base"

VERSION="$(python3 -c "import tomllib; print(tomllib.load(open('$ROOT/pyproject.toml','rb'))['project']['version'])")"

platforms=(mac linux win)

stage_base() {
  rm -rf "$BASE_STAGE"
  mkdir -p "$BASE_STAGE/console/web" "$RELEASE_DIR"

  rsync -a "$ROOT/pyproject.toml" "$BASE_STAGE/"
  rsync -a \
    --exclude '.DS_Store' \
    --exclude '*.local.yaml' \
    --exclude 'providers.local.yaml' \
    --exclude 'api.local.yaml' \
    --exclude 'llm_models.local.yaml' \
    --exclude 'google-oauth-client.json' \
    --exclude 'agents.local.yaml' \
    "$ROOT/config/" "$BASE_STAGE/config/"
  rsync -a --exclude '__pycache__' --exclude '*.pyc' "$ROOT/niannian/" "$BASE_STAGE/niannian/"
  rsync -a \
    --exclude 'download/releases/*.zip' \
    --exclude 'download/releases/.stage' \
    --exclude 'download/releases/.stage-base' \
    "$ROOT/client/" "$BASE_STAGE/client/"
  rsync -a "$ROOT/console/web/dist/" "$BASE_STAGE/console/web/dist/"
}

write_mac_extras() {
  local stage="$1"
  cat > "$stage/INSTALL.txt" <<EOF
念念客户端 macOS v${VERSION}

1. 解压到任意目录
2. 终端执行：bash client/mac/install.sh
   或双击「安装.command」（若系统允许）
3. 打开 http://127.0.0.1:18765/niannian/settings

需要 Python 3.10+。安装包已含 Console，无需 git / npm。
EOF
  cat > "$stage/安装.command" <<'EOF'
#!/bin/bash
cd "$(dirname "$0")"
bash client/mac/install.sh
EOF
  chmod +x "$stage/安装.command"
}

write_linux_extras() {
  local stage="$1"
  cat > "$stage/INSTALL.txt" <<EOF
念念客户端 Linux v${VERSION}

1. 解压：unzip niannian-client-linux.zip -d NianNian && cd NianNian
2. 执行：bash install.sh
3. 打开 http://127.0.0.1:18765/niannian/settings

需要 Python 3.10+。安装包已含 Console，无需 git / npm。
EOF
  cat > "$stage/install.sh" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")" && pwd)"
exec bash "$ROOT/client/linux/install.sh"
EOF
  chmod +x "$stage/install.sh"
}

write_win_extras() {
  local stage="$1"
  cat > "$stage/INSTALL.txt" <<EOF
念念客户端 Windows v${VERSION}

1. 解压到例如 C:\NianNian
2. 右键「install.cmd」以管理员身份运行，或在 PowerShell 中：
   powershell -ExecutionPolicy Bypass -File client\windows\install.ps1
3. 打开 http://127.0.0.1:18765/niannian/settings

需要 Python 3.10+。安装包已含 Console，无需 git / npm。
EOF
  cat > "$stage/install.cmd" <<'EOF'
@echo off
cd /d "%~dp0"
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0client\windows\install.ps1"
pause
EOF
}

zip_platform() {
  local platform="$1"
  local stage="$RELEASE_DIR/.stage-${platform}"
  local zip_latest="$RELEASE_DIR/niannian-client-${platform}.zip"
  local zip_versioned="$RELEASE_DIR/niannian-client-${platform}-${VERSION}.zip"

  rm -rf "$stage"
  cp -R "$BASE_STAGE" "$stage"

  case "$platform" in
    mac) write_mac_extras "$stage" ;;
    linux) write_linux_extras "$stage" ;;
    win) write_win_extras "$stage" ;;
  esac

  (cd "$stage" && zip -qr "$zip_versioned" .)
  cp "$zip_versioned" "$zip_latest"
  rm -rf "$stage"

  local bytes
  bytes="$(wc -c < "$zip_latest" | tr -d ' ')"
  echo "==> ${platform}: $zip_latest (${bytes} bytes)"
}

echo "==> NianNian client Pages release (v${VERSION})"
bash "$ROOT/client/scripts/build-console-client.sh"
stage_base

for p in "${platforms[@]}"; do
  zip_platform "$p"
done

rm -rf "$BASE_STAGE"
echo "Deploy: upload client/download/ to Cloudflare Pages"
