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

# Exports the current local Postgres (docker-compose db) schema + data to a SQL file.
# Usage:
#   ./scripts/supabase_export.sh [output_file]
#
# Default output: ./supabase_dump.sql

OUTPUT_FILE="${1:-supabase_dump.sql}"

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

# Local DB connection (matches docker-compose)
DB_HOST="${DB_HOST:-localhost}"
DB_PORT="${DB_PORT:-5432}"
DB_NAME="${DB_NAME:-tipsharks}"
DB_USER="${DB_USER:-tipsharks}"
DB_PASSWORD="${DB_PASSWORD:-tipsharks}"

export PGPASSWORD="${DB_PASSWORD}"

pg_dump \
  --host "${DB_HOST}" \
  --port "${DB_PORT}" \
  --username "${DB_USER}" \
  --dbname "${DB_NAME}" \
  --format=p \
  --no-owner \
  --no-privileges \
  --clean \
  --if-exists \
  > "${OUTPUT_FILE}"

echo "Exported schema + data to ${OUTPUT_FILE}"
