#!/usr/bin/env bash
#
# Restore-test scaffold. Run by hand against a *test* Supabase project to
# confirm a backup is restorable. We intentionally do NOT automate this —
# pointing a script at the production DB by mistake would be unrecoverable.
#
# Usage:
#   scripts/backup-restore-check.sh <path/to/backup.sql.gz> <test-db-url>

set -euo pipefail

if [[ $# -lt 2 ]]; then
  echo "Usage: $0 <path/to/backup.sql.gz> <test-db-url>" >&2
  exit 2
fi

BACKUP="$1"
TEST_DB="$2"

if [[ "$TEST_DB" == *prod* || "$TEST_DB" == *production* ]]; then
  echo "Refusing to restore: target URL looks like production." >&2
  exit 3
fi

if [[ ! -f "$BACKUP" ]]; then
  echo "Backup file not found: $BACKUP" >&2
  exit 4
fi

echo "Restoring ${BACKUP} → ${TEST_DB}"
gunzip -c "$BACKUP" | psql "$TEST_DB"

echo "Counting tables…"
psql "$TEST_DB" -c "select table_schema, count(*) as table_count
  from information_schema.tables
  where table_schema in ('public','auth')
  group by table_schema
  order by table_schema;"

echo "Done. Manually verify row counts on key tables before declaring backup healthy."
