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

# Stonks Oracle — Database Restore
# Restores a pg_dump backup into the stonks database.
#
# Usage:
#   ./scripts/restore-db.sh <backup-file.sql.gz>
#   ./scripts/restore-db.sh ~/backups/stonks-oracle/stonks-20260415-180000.sql.gz
#
# WARNING: This will DROP and recreate all tables in the stonks database.
#          All existing data will be replaced with the backup contents.
#
# Requires: kubectl access to the cluster

NAMESPACE_PG="postgresql-service"
PG_POD="postgresql-1"
PG_USER="postgres"
PG_DB="stonks"

if [ $# -lt 1 ]; then
  echo "Usage: $0 <backup-file.sql.gz>"
  echo ""
  echo "Available backups:"
  ls -lh "${HOME}/backups/stonks-oracle"/stonks-*.sql.gz 2>/dev/null || echo "  (none found in ~/backups/stonks-oracle/)"
  exit 1
fi

BACKUP_FILE="$1"

if [ ! -f "${BACKUP_FILE}" ]; then
  echo "ERROR: Backup file not found: ${BACKUP_FILE}"
  exit 1
fi

BACKUP_SIZE=$(du -h "${BACKUP_FILE}" | cut -f1)
TABLE_COUNT=$(zcat "${BACKUP_FILE}" | grep -c "^CREATE TABLE" || true)

echo "=== Stonks Oracle Database Restore ==="
echo "Backup file: ${BACKUP_FILE}"
echo "File size:   ${BACKUP_SIZE}"
echo "Tables:      ${TABLE_COUNT}"
echo ""
echo "WARNING: This will replace ALL data in the '${PG_DB}' database."
echo ""
read -p "Are you sure? Type 'yes' to continue: " CONFIRM

if [ "${CONFIRM}" != "yes" ]; then
  echo "Aborted."
  exit 0
fi

# Terminate active connections to the database
echo "[1/4] Terminating active connections..."
kubectl exec -n "${NAMESPACE_PG}" "${PG_POD}" -c postgres -- \
  psql -U "${PG_USER}" -c \
  "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '${PG_DB}' AND pid <> pg_backend_pid();" \
  2>/dev/null || true

# Scale down all stonks-oracle deployments to prevent reconnections
echo "[2/4] Scaling down stonks-oracle services..."
for dep in $(kubectl get deployments -n stonks-oracle -o name 2>/dev/null); do
  kubectl scale -n stonks-oracle "$dep" --replicas=0 2>/dev/null || true
done
echo "       Waiting for pods to terminate..."
sleep 10

# Restore the backup
echo "[3/4] Restoring database from backup..."
zcat "${BACKUP_FILE}" | kubectl exec -i -n "${NAMESPACE_PG}" "${PG_POD}" -c postgres -- \
  psql -U "${PG_USER}" -d "${PG_DB}" --single-transaction 2>&1 | \
  grep -v "^SET$\|^COMMENT$\|^ALTER\|^DROP\|^CREATE\|^--" | head -20

# Re-grant permissions to the stonks user
echo "       Re-granting permissions..."
kubectl exec -n "${NAMESPACE_PG}" "${PG_POD}" -c postgres -- \
  psql -U "${PG_USER}" -d "${PG_DB}" -c "
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO stonks;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO stonks;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO stonks;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO stonks;
"

# Verify restore
echo "       Verifying..."
RESTORED_TABLES=$(kubectl exec -n "${NAMESPACE_PG}" "${PG_POD}" -c postgres -- \
  psql -U "${PG_USER}" -d "${PG_DB}" -t -c \
  "SELECT count(*) FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE';" | tr -d ' ')
echo "       Restored tables: ${RESTORED_TABLES}"

# Scale services back up
echo "[4/4] Scaling stonks-oracle services back up..."
for dep in $(kubectl get deployments -n stonks-oracle -o name 2>/dev/null); do
  kubectl scale -n stonks-oracle "$dep" --replicas=1 2>/dev/null || true
done

# Scale ingestion and parser to 2 replicas
kubectl scale -n stonks-oracle deployment/ingestion --replicas=2 2>/dev/null || true
kubectl scale -n stonks-oracle deployment/parser --replicas=2 2>/dev/null || true

echo ""
echo "=== Restore complete ==="
echo "Tables restored: ${RESTORED_TABLES}"
echo "Services are scaling back up. Check with:"
echo "  kubectl get pods -n stonks-oracle"
