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

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck disable=SC1091
source "$SCRIPT_DIR/pd_backup_lib.sh"

verify_root="${RESTORE_VERIFY_WORK_ROOT:-/mnt/tank/docker/backups/restore-verify}"
dbs="${RESTORE_VERIFY_DB_LIST:-${POSTGRES_DB_LIST:-}}"

if [[ -z "$dbs" ]]; then
  echo "RESTORE_VERIFY_DB_LIST and POSTGRES_DB_LIST are both empty." >&2
  exit 1
fi

mkdir -p "$verify_root"
stamp="$(timestamp_utc)"
container="pd-restore-verify-$stamp"
password="verify-$stamp"

if [[ -n "${RESTORE_VERIFY_POSTGRES_IMAGE:-}" ]]; then
  verify_image="$RESTORE_VERIFY_POSTGRES_IMAGE"
else
  source_major="$(docker_cmd exec "$POSTGRES_CONTAINER" postgres -V | awk '{print $3}' | cut -d. -f1)"
  verify_image="postgres:${source_major}-alpine"
fi

cleanup() {
  docker_cmd rm -f "$container" >/dev/null 2>&1 || true
}
trap cleanup EXIT

echo "Starting temporary Postgres verifier container $container"
docker_cmd run -d --rm \
  --name "$container" \
  -e POSTGRES_PASSWORD="$password" \
  "$verify_image" >/dev/null

echo "Waiting for temporary Postgres to accept connections"
for _ in {1..30}; do
  if docker_cmd exec "$container" pg_isready -U postgres >/dev/null 2>&1; then
    break
  fi
  sleep 2
done

docker_cmd exec "$container" pg_isready -U postgres >/dev/null

for db in $dbs; do
  dump_file="$(latest_dump_for_db "$db")"
  if [[ -z "$dump_file" ]]; then
    echo "No dump found for database '$db' under $PD_DB_DUMP_ROOT" >&2
    exit 1
  fi

  echo "Restoring $dump_file into temporary database '$db'"
  while IFS= read -r role_name; do
    [[ -z "$role_name" ]] && continue
    docker_cmd exec "$container" createuser -U postgres "$role_name" >/dev/null 2>&1 || true
  done < <(gunzip -c "$dump_file" | sed -n "s/.*OWNER TO \\([^;]*\\);/\\1/p" | sort -u)

  docker_cmd exec "$container" createdb -U postgres "$db"
  gunzip -c "$dump_file" | docker_cmd exec -i "$container" psql -v ON_ERROR_STOP=1 -U postgres -d "$db" >/dev/null

  table_count="$(docker_cmd exec "$container" psql -At -U postgres -d "$db" -c "SELECT count(*) FROM information_schema.tables WHERE table_schema NOT IN ('pg_catalog','information_schema');")"
  echo "Verified database '$db' with $table_count non-system tables."
done

echo "Postgres restore verification passed."
