#!/bin/sh
set -eu

ROOT=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
cd "$ROOT"

row_count=${DB_SCALE_ROWS:-50000}

case "$row_count" in
  ''|*[!0-9]*)
    echo "DB_SCALE_ROWS must be an integer greater than or equal to 3." >&2
    exit 2
    ;;
esac

if [ "$row_count" -lt 3 ]; then
  echo "DB_SCALE_ROWS must be greater than or equal to 3." >&2
  exit 2
fi

run_id="$(date -u +%Y%m%d%H%M%S)-$$"
project="wnh_db_scale_$(printf '%s' "$run_id" | tr -d '-')"
image="who-need-help:db-scale-$run_id"
postgis_image="who-need-help:db-scale-postgis-$run_id"
output="$ROOT/output/db-scale/$run_id"

umask 077
QUALITY_POSTGRES_USER="wnh_scale_$(openssl rand -hex 6)"
QUALITY_POSTGRES_PASSWORD=$(openssl rand -base64 48 | tr -d '\n')
QUALITY_POSTGIS_IMAGE=$postgis_image
export QUALITY_POSTGRES_USER QUALITY_POSTGRES_PASSWORD QUALITY_POSTGIS_IMAGE

compose="docker compose -p $project -f $ROOT/compose.quality.yaml"

cleanup() {
  $compose down --volumes --remove-orphans >/dev/null 2>&1 || true
  docker image rm "$image" "$postgis_image" >/dev/null 2>&1 || true
}
trap cleanup EXIT HUP INT TERM

mkdir -p "$output"

echo "Building the isolated database-scale image"
docker build --target test --tag "$image" .
docker build --tag "$postgis_image" --file Dockerfile.postgis .

echo "Starting an isolated PostgreSQL/PostGIS 18 database"
$compose up --detach --wait db

run_mix() {
  docker run --rm \
    --network "${project}_internal" \
    --env MIX_ENV=test \
    --env APP_ROLE=migrate \
    --env DB_HOST=db \
    --env "DB_USER=$QUALITY_POSTGRES_USER" \
    --env "DB_PASSWORD=$QUALITY_POSTGRES_PASSWORD" \
    --volume "$output:/benchmark-output" \
    "$image" "$@"
}

echo "Migrating to the pre-index baseline"
run_mix mix ecto.create
run_mix mix ecto.migrate --to 20260719004249

echo "Seeding and measuring $row_count rows per large benchmark table"
run_mix mix wnh.db_scale_benchmark \
  --phase before \
  --rows "$row_count" \
  --output /benchmark-output

echo "Applying the generated cursor-index migration and measuring again"
run_mix mix ecto.migrate
run_mix mix wnh.db_scale_benchmark \
  --phase after \
  --rows "$row_count" \
  --output /benchmark-output

docker run --rm \
  --volume "$output:/output" \
  alpine:3.23.3@sha256:25109184c71bdad752c8312a8623239686a9a2071e8825f20acb8f2198c3f659 \
  chown -R "$(id -u):$(id -g)" /output

echo "Isolated before/after plans and measurements: $output"
