#!/usr/bin/env bash
# Copies the bill-tracker backend into mobile/nodejs-assets/nodejs-project/server/
# so it can be bundled into the Android app and run by nodejs-mobile (Node 18) in
# local mode.
#
# The backend is now TypeScript (server.cts + .cts/.mts). Node 18 can't run TS, so
# this transpiles the server's own sources to CommonJS in place (keeping filenames;
# see scripts/transpile-node18.js). Dependencies run as-is on Node 18 and are NOT
# transpiled — install them separately (see the "Next" steps printed at the end).
#
# Usage: ./scripts/sync-nodejs-project.sh [--source /path/to/bill-tracker]
# --source defaults to ../bill-tracker (sibling directory). Run from mobile root.

set -euo pipefail

MOBILE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

REPO_ROOT=""
while [[ $# -gt 0 ]]; do
  case "$1" in
    --source) REPO_ROOT="$(cd "$2" && pwd)"; shift 2 ;;
    *) echo "Unknown arg: $1" >&2; exit 1 ;;
  esac
done
if [[ -z "$REPO_ROOT" ]]; then
  REPO_ROOT="$(cd "$MOBILE_DIR/../bill-tracker" && pwd)"
fi

if [[ ! -f "$REPO_ROOT/server.cts" ]]; then
  echo "Error: Bill Tracker source not found at $REPO_ROOT (no server.cts)." >&2
  echo "Use --source /path/to/bill-tracker to specify the project root." >&2
  exit 1
fi

DEST="$MOBILE_DIR/nodejs-assets/nodejs-project/server"

echo "Syncing backend source from $REPO_ROOT -> $DEST"
rm -rf "$DEST"
mkdir -p "$DEST"

cp "$REPO_ROOT/server.cts" "$DEST/"

# Backend source trees. `types/` is type-only (erased by the transpile) but cheap
# insurance; `setup/` holds firstRun.cts (required by server.cts on empty DB).
for dir in db routes services middleware utils workers setup types; do
  [[ -d "$REPO_ROOT/$dir" ]] && cp -R "$REPO_ROOT/$dir" "$DEST/$dir"
done

# routes/user.cts requires ../scripts/seedDemoData.cts (relative to server/).
mkdir -p "$DEST/scripts"
cp "$REPO_ROOT/scripts/seedDemoData.cts" "$DEST/scripts/"

# Seed JSON read at first-run init:
#   db/data/*.json          -> carried with db/ above (database.cts reads them)
#   docs/top_200_*.json     -> db/migrations/versionedMigrations.cts reads ../../docs/
mkdir -p "$DEST/docs"
cp "$REPO_ROOT/docs/top_200_us_subscriptions_researched_2026-06-06.json" "$DEST/docs/"

# Drop dev SQLite DBs and any stray node_modules that rode along with db/.
rm -f "$DEST"/db/*.db "$DEST"/db/*.db-* 2>/dev/null || true
rm -rf "$DEST/db/node_modules"

# Transpile the server's TypeScript sources to CommonJS in place (Node 18).
# Runs BEFORE dist/ is copied so the browser ESM bundle isn't touched.
node "$MOBILE_DIR/scripts/transpile-node18.js" "$DEST"

# Built frontend, served as static files by server.cts (path.join(__dirname,'dist')).
cp -R "$REPO_ROOT/dist" "$DEST/dist"

cat <<EOF
Done syncing server source.

Next (only when deps changed):
  1. cd "$MOBILE_DIR/nodejs-assets/nodejs-project" && npm install
  2. node "$MOBILE_DIR/scripts/prepare-local-mode-deps.js"   # kysely->CJS, stub openid-client
  3. "$MOBILE_DIR/scripts/install-node18-runtime.sh"          # (re)apply Node 18 runtime + bridge patch
  4. "$MOBILE_DIR/scripts/build-better-sqlite3-node18.sh"     # better-sqlite3 for the Node 18 ABI
EOF
