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

SOURCE_DB="${SOURCE_DB:-inventory_data}"
TARGET_DB="${TARGET_DB:-inventory}"
PGHOST="${PGHOST:-localhost}"
PGPORT="${PGPORT:-5433}"
PGUSER="${PGUSER:-postgres}"
PGPASSWORD="${PGPASSWORD:-postgres}"
DUMP_FILE="${DUMP_FILE:-/tmp/inventory_data_dump.sql}"

export PGPASSWORD

EXCLUDE_TABLES=(
  "doctrine_migration_versions"
  "migration_versions"
  "_prisma_migrations"
  "profiles"
)

EXCLUDE_ARGS=()
for table in "${EXCLUDE_TABLES[@]}"; do
  EXCLUDE_ARGS+=(--exclude-table-data="$table")
done

echo "Dumping data from ${SOURCE_DB}..."
pg_dump \
  --data-only \
  --inserts \
  --no-owner \
  --no-privileges \
  "${EXCLUDE_ARGS[@]}" \
  -h "${PGHOST}" \
  -p "${PGPORT}" \
  -U "${PGUSER}" \
  "${SOURCE_DB}" > "${DUMP_FILE}"

echo "Restoring data into ${TARGET_DB}..."
psql \
  -h "${PGHOST}" \
  -p "${PGPORT}" \
  -U "${PGUSER}" \
  "${TARGET_DB}" < "${DUMP_FILE}"

echo "Done. Data copied from ${SOURCE_DB} to ${TARGET_DB}."
