#!/usr/bin/env bash
# deploy.sh — build then sync bill-tracker to server
# Usage: ./deploy.sh user@nebula [remote-path]

set -euo pipefail

SERVER="${1:?Usage: ./deploy.sh user@nebula [remote-path]}"
REMOTE_PATH="${2:-/opt/bill-tracker}"

# ── Pre-flight ────────────────────────────────────────────────────────────────
if [ ! -f package.json ]; then
  echo "ERROR: Run this from the bill-tracker project root"
  exit 1
fi

if ls db/*.db 2>/dev/null | grep -q .; then
  echo "✓ Local DB found — excluded from sync (data stays on server)"
fi

# ── Build React app ───────────────────────────────────────────────────────────
echo "→ Building UI..."
npm run build

# ── Sync (dist/ is included, src/ and public/ are excluded) ──────────────────
echo "→ Syncing to ${SERVER}:${REMOTE_PATH}"

rsync -avz --delete \
  --exclude-from='.rsyncignore' \
  ./ "${SERVER}:${REMOTE_PATH}/"

echo "✓ Done"
