#!/usr/bin/env bash
# GM-Relay PostgreSQL Backup Restore Script
# Usage: ./scripts/restore.sh [backup_file]
# If no file is provided, uses the most recent backup.

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"

# Check required env
if [ -z "${POSTGRES_PASSWORD:-}" ]; then
    if [ -f "${PROJECT_ROOT}/.env" ]; then
        # shellcheck source=/dev/null
        set -a
        . "${PROJECT_ROOT}/.env"
        set +a
    fi
fi

if [ -z "${POSTGRES_PASSWORD:-}" ]; then
    echo "ERROR: POSTGRES_PASSWORD is not set. Please set it in your environment or .env file."
    exit 1
fi

BACKUP_DIR="${PROJECT_ROOT}/backups"

# Determine backup file
if [ $# -ge 1 ]; then
    BACKUP_FILE="$1"
else
    BACKUP_FILE=$(find "${BACKUP_DIR}" -name 'gmrelay_db_*.sql.gz' -type f -printf '%T+ %p\n' 2>/dev/null | sort -r | head -n1 | cut -d' ' -f2-)
    if [ -z "${BACKUP_FILE}" ]; then
        echo "ERROR: No backup files found in ${BACKUP_DIR}."
        exit 1
    fi
fi

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

echo "=================================================="
echo "  GM-Relay PostgreSQL Restore"
echo "=================================================="
echo ""
echo "Backup file: ${BACKUP_FILE}"
echo "Database:    gmrelay_db"
echo "User:        gmrelay"
echo ""
read -p "This will OVERWRITE the current database. Are you sure? [y/N] " CONFIRM

if [[ ! "${CONFIRM}" =~ ^[Yy]$ ]]; then
    echo "Restore cancelled."
    exit 0
fi

echo ""
echo "Restoring database from ${BACKUP_FILE}..."

# Restore using docker compose exec to leverage the running postgres container
COMPOSE_ARGS="-f ${PROJECT_ROOT}/compose.yaml"

docker compose ${COMPOSE_ARGS} exec -T -e PGPASSWORD="${POSTGRES_PASSWORD}" db psql \
    -U gmrelay \
    -d gmrelay_db \
    -c "DROP SCHEMA public CASCADE; CREATE SCHEMA public;" 2>/dev/null || true

gunzip -c "${BACKUP_FILE}" | docker compose ${COMPOSE_ARGS} exec -T -e PGPASSWORD="${POSTGRES_PASSWORD}" db psql \
    -U gmrelay \
    -d gmrelay_db

echo ""
echo "=================================================="
echo "  Restore completed successfully!"
echo "=================================================="
