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

# Imports a SQL dump into Supabase (or any Postgres) using psql.
# Usage:
#   SUPABASE_DB_URL="postgresql://USER:PASSWORD@HOST:PORT/DB?sslmode=require" \
#     ./scripts/supabase_import.sh supabase_dump.sql

if [ $# -lt 1 ]; then
  echo "Usage: SUPABASE_DB_URL=... ./scripts/supabase_import.sh <dump.sql>" >&2
  exit 1
fi

DUMP_FILE="$1"
if [ ! -f "${DUMP_FILE}" ]; then
  echo "Dump file not found: ${DUMP_FILE}" >&2
  exit 1
fi

if ! command -v psql >/dev/null 2>&1; then
  echo "psql not found. Install PostgreSQL client tools first." >&2
  exit 1
fi

if [ -z "${SUPABASE_DB_URL:-}" ]; then
  echo "SUPABASE_DB_URL is not set." >&2
  exit 1
fi

psql "${SUPABASE_DB_URL}" -f "${DUMP_FILE}"

echo "Imported ${DUMP_FILE} into Supabase."
