#!/usr/bin/env bash
# Copies the bill-tracker backend source into mobile/nodejs-assets/nodejs-project/server/
# so it can be bundled into the Android app and run by nodejs-mobile in local mode.
#
# Usage: ./scripts/sync-nodejs-project.sh [--source /path/to/bill-tracker]
# Run from the mobile project root.
#
# --source defaults to ../bill-tracker (sibling directory).

set -euo pipefail

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

# Parse --source flag
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.js" ]]; then
  echo "Error: Bill Tracker source not found at $REPO_ROOT" >&2
  echo "Use --source /path/to/bill-tracker to specify the Bill Tracker 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.js" "$DEST/"

for dir in db routes services middleware utils workers; do
  cp -R "$REPO_ROOT/$dir" "$DEST/$dir"
done

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

# Drop dev SQLite DB files and node_modules if they were copied along with db/.
rm -f "$DEST"/db/*.db "$DEST"/db/*.db-* "$DEST"/db/*.db-shm "$DEST"/db/*.db-wal
rm -rf "$DEST/db/node_modules"

# Seed-data JSON files read by db/database.js during first-run initialization.
mkdir -p "$DEST/docs"
cp "$REPO_ROOT/docs/advisory_non_bill_transaction_filters_us_ms_5000.json" "$DEST/docs/"
cp "$REPO_ROOT/docs/top_200_us_subscriptions_researched_2026-06-06.json" "$DEST/docs/"
cp "$REPO_ROOT/docs/merchant_store_match_us_nems_online_5k_v0_2.json" "$DEST/docs/"

# nodejs-mobile-cordova 0.4.3 embeds Node 12.19, which doesn't support
# optional chaining (?.) / nullish coalescing (??) used in this project and
# in some npm dependencies (e.g. express-rate-limit). Transpile server-side
# .js files down to Node 12-compatible syntax in place. Runs before the
# frontend dist/ is copied below so its browser ESM bundle isn't rewritten
# to CJS (which breaks with "module is not defined" in the WebView).
node "$MOBILE_DIR/scripts/transpile-node12.js" "$DEST"

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

# Also transpile already-installed dependencies, if present (run
# `npm install` in nodejs-assets/nodejs-project after this script to pick up
# new/updated deps, then re-run this script to transpile them).
NODE_MODULES="$MOBILE_DIR/nodejs-assets/nodejs-project/node_modules"
if [ -d "$NODE_MODULES" ]; then
  node "$MOBILE_DIR/scripts/transpile-node12.js" "$NODE_MODULES"
fi

echo "Done. Next: cd \"$MOBILE_DIR/nodejs-assets/nodejs-project\" && npm install"
