#!/usr/bin/env bash
set -euo pipefail

# ─── Database Migration: Homelab PostgreSQL → Supabase ────────
#
# Prerequisites:
#   - pg_dump and pg_restore installed locally
#   - Network access to both the homelab PG and Supabase PG
#   - Supabase project created with direct connection string
#
# Usage:
#   HOMELAB_DB_URL="postgresql://echos_ocr:PASSWORD@192.168.68.102:5432/echos_ocr" \
#   SUPABASE_DB_URL="postgresql://postgres.REF:PASSWORD@db.REF.supabase.co:5432/postgres" \
#   bash scripts/migrate-database.sh

if [ -z "${HOMELAB_DB_URL:-}" ]; then
  echo "Error: HOMELAB_DB_URL is not set"
  echo "Example: postgresql://echos_ocr:PASSWORD@192.168.68.102:5432/echos_ocr"
  exit 1
fi

if [ -z "${SUPABASE_DB_URL:-}" ]; then
  echo "Error: SUPABASE_DB_URL is not set (use the DIRECT connection, port 5432, not pooled)"
  echo "Example: postgresql://postgres.REF:PASSWORD@db.REF.supabase.co:5432/postgres"
  exit 1
fi

DUMP_FILE="echos_ocr_backup_$(date +%Y%m%d_%H%M%S).dump"

echo "=== Step 1: Dumping homelab database ==="
pg_dump "${HOMELAB_DB_URL}" -Fc -f "${DUMP_FILE}"
echo "Dump created: ${DUMP_FILE} ($(du -h "${DUMP_FILE}" | cut -f1))"

echo ""
echo "=== Step 2: Restoring to Supabase ==="
pg_restore "${SUPABASE_DB_URL}" --no-owner --no-privileges --clean --if-exists "${DUMP_FILE}" 2>&1 || true
echo "Restore complete (some warnings about existing objects are normal)"

echo ""
echo "=== Step 3: Pushing schema with Prisma ==="
echo "Running 'npx prisma db push' to ensure schema alignment..."
DATABASE_URL="${SUPABASE_DB_URL}" npx prisma db push --skip-generate 2>&1
echo "Schema push complete"

echo ""
echo "=== Step 4: Verifying row counts ==="
psql "${SUPABASE_DB_URL}" -c "
SELECT 'User' as table_name, count(*) FROM \"User\"
UNION ALL SELECT 'Organization', count(*) FROM \"Organization\"
UNION ALL SELECT 'OrgMember', count(*) FROM \"OrgMember\"
UNION ALL SELECT 'ResponseCard', count(*) FROM \"ResponseCard\"
UNION ALL SELECT 'ProcessingJob', count(*) FROM \"ProcessingJob\"
UNION ALL SELECT 'ActivityLog', count(*) FROM \"ActivityLog\"
UNION ALL SELECT 'Notification', count(*) FROM \"Notification\"
UNION ALL SELECT 'Integration', count(*) FROM \"Integration\"
ORDER BY table_name;
"

echo ""
echo "=== Compare with homelab counts ==="
psql "${HOMELAB_DB_URL}" -c "
SELECT 'User' as table_name, count(*) FROM \"User\"
UNION ALL SELECT 'Organization', count(*) FROM \"Organization\"
UNION ALL SELECT 'OrgMember', count(*) FROM \"OrgMember\"
UNION ALL SELECT 'ResponseCard', count(*) FROM \"ResponseCard\"
UNION ALL SELECT 'ProcessingJob', count(*) FROM \"ProcessingJob\"
UNION ALL SELECT 'ActivityLog', count(*) FROM \"ActivityLog\"
UNION ALL SELECT 'Notification', count(*) FROM \"Notification\"
UNION ALL SELECT 'Integration', count(*) FROM \"Integration\"
ORDER BY table_name;
"

echo ""
echo "=== Migration complete ==="
echo "Backup file retained at: ${DUMP_FILE}"
echo "Verify the counts above match between homelab and Supabase."
