#!/bin/bash
# Deploy CHL Effectiveness Dashboard to homeserver
# Usage: ./deploy-to-homeserver.sh
#
# Prerequisites:
#   - SSH access to homeserver (ssh kim@homeserver)
#   - Tailscale running on both machines

set -e

SERVER="kim@homeserver"
REMOTE_DIR="/home/kim/projects/chl-effectiveness"
LOCAL_DIR="$(cd "$(dirname "$0")" && pwd)"

echo "=== CHL Effectiveness Dashboard — Deploy to Homeserver ==="
echo ""

# Step 1: Ensure remote directory exists
echo "[1/5] Creating remote directory..."
ssh "$SERVER" "mkdir -p $REMOTE_DIR/.claude/context $REMOTE_DIR/.claude/skills/supabase-connector $REMOTE_DIR/photos/journal"

# Step 2: Sync files
echo "[2/5] Syncing files to homeserver..."
rsync -avz --progress \
  "$LOCAL_DIR/dashboard-server.py" \
  "$LOCAL_DIR/dashboard.html" \
  "$LOCAL_DIR/upload.html" \
  "$SERVER:$REMOTE_DIR/"

# Sync context files
rsync -avz --progress \
  "$LOCAL_DIR/.claude/context/personal-profile.md" \
  "$LOCAL_DIR/.claude/context/activity-config.json" \
  "$LOCAL_DIR/.claude/context/merchant-categories.json" \
  "$LOCAL_DIR/.claude/context/psychological-patterns.md" \
  "$LOCAL_DIR/.claude/context/anthropic-config.json" \
  "$SERVER:$REMOTE_DIR/.claude/context/"

# Sync optional context files (ignore if missing)
for f in daily-planning-context.md health-context.md financial-context.md life-vision.md dashboard-design-system.md; do
  if [ -f "$LOCAL_DIR/.claude/context/$f" ]; then
    rsync -avz "$LOCAL_DIR/.claude/context/$f" "$SERVER:$REMOTE_DIR/.claude/context/"
  fi
done

# Sync house-admin.json if it exists
if [ -f "$LOCAL_DIR/.claude/context/house-admin.json" ]; then
  rsync -avz "$LOCAL_DIR/.claude/context/house-admin.json" "$SERVER:$REMOTE_DIR/.claude/context/"
fi

# Sync supabase connector
rsync -avz --progress \
  "$LOCAL_DIR/.claude/skills/supabase-connector/client.py" \
  "$LOCAL_DIR/.claude/skills/supabase-connector/schema.sql" \
  "$LOCAL_DIR/.claude/skills/supabase-connector/config.json" \
  "$SERVER:$REMOTE_DIR/.claude/skills/supabase-connector/"

# Sync data files if they exist
for f in whoop-data.json today-context.json; do
  if [ -f "$LOCAL_DIR/$f" ]; then
    rsync -avz "$LOCAL_DIR/$f" "$SERVER:$REMOTE_DIR/"
  fi
done

# Sync savings-goals.json if exists
if [ -f "$LOCAL_DIR/.claude/context/savings-goals.json" ]; then
  rsync -avz "$LOCAL_DIR/.claude/context/savings-goals.json" "$SERVER:$REMOTE_DIR/.claude/context/"
fi

# Step 3: Ensure venv + Python dependencies on server
echo "[3/5] Ensuring venv and Python dependencies..."
ssh "$SERVER" "cd $REMOTE_DIR && python3 -m venv venv 2>/dev/null; source venv/bin/activate && pip install -q supabase anthropic python-dateutil pytz"

# Step 4: Start/restart with PM2 (using venv Python)
echo "[4/5] Starting dashboard with PM2..."
ssh "$SERVER" "cd $REMOTE_DIR && pm2 delete chl-dashboard 2>/dev/null; pm2 start dashboard-server.py --name chl-dashboard --interpreter $REMOTE_DIR/venv/bin/python3 && pm2 save"

# Step 5: Set up Tailscale Serve (private access via Tailscale network)
echo "[5/5] Configuring Tailscale Serve on port 8765..."
ssh "$SERVER" "sudo tailscale serve --bg 8765"

echo ""
echo "=== Deployment Complete ==="
echo ""
echo "Access your CHL dashboard:"
echo "  Local (home network): http://192.168.86.50:8765/dashboard.html"
echo "  Tailscale (anywhere):  http://homeserver:8765/dashboard.html"
echo "  HTTPS (anywhere):      https://homeserver.tail03fd12.ts.net:8765/dashboard.html"
echo ""
echo "Manage:"
echo "  pm2 logs chl-dashboard     # View logs"
echo "  pm2 restart chl-dashboard  # Restart"
echo "  pm2 status                 # Check status"
echo ""
echo "Re-deploy after changes:"
echo "  ./deploy-to-homeserver.sh"
