#!/bin/sh
set -eu

ROOT=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
ENV_FILE="$ROOT/.env.load"
REPLICAS=${1:-}

"$ROOT/scripts/ensure-local-load-env.sh"

set -a
# shellcheck source=/dev/null
. "$ENV_FILE"
set +a

: "${LOAD_PROJECT:?LOAD_PROJECT is missing from .env.load}"
: "${LOAD_HOST:?LOAD_HOST is missing from .env.load}"
: "${LOAD_WEB_REPLICAS:?LOAD_WEB_REPLICAS is missing from .env.load}"
: "${LOAD_WORKER_REPLICAS:?LOAD_WORKER_REPLICAS is missing from .env.load}"

if [ "$LOAD_PROJECT" = who_need_help ]; then
  echo "The load profile must not use the staging Compose project." >&2
  exit 1
fi

if [ -n "$REPLICAS" ]; then
  LOAD_WEB_REPLICAS=$REPLICAS
fi

case "$LOAD_WEB_REPLICAS:$LOAD_WORKER_REPLICAS" in
  *[!0-9:]* | 0:* | *:0 | :* | *:)
    echo "Web and worker replica counts must be positive integers." >&2
    exit 1
    ;;
esac

cd "$ROOT"
docker build --target load_tools --tag who-need-help:load-tools .

docker compose \
  --env-file "$ENV_FILE" \
  -p "$LOAD_PROJECT" \
  -f compose.yaml \
  -f compose.load.yaml \
  up -d --build --wait \
  --scale "web=$LOAD_WEB_REPLICAS" \
  --scale "worker=$LOAD_WORKER_REPLICAS"

COMPOSE_PROJECT_NAME=$LOAD_PROJECT "$ROOT/scripts/verify-realtime-cluster.sh" compose
COMPOSE_PROJECT_NAME=$LOAD_PROJECT "$ROOT/scripts/verify-beam-runtime.sh" compose
status=$(
  curl --silent --show-error \
    --header "Host: $LOAD_HOST" \
    --output /dev/null \
    --write-out '%{http_code}' \
    "http://localhost:$HTTP_PORT/healthz/ready"
)

if [ "$status" != 200 ]; then
  echo "Load-profile readiness returned HTTP $status instead of 200." >&2
  exit 1
fi

curl --fail --silent --show-error \
  --header "Host: $LOAD_HOST" \
  "http://localhost:$HTTP_PORT/healthz/ready"
printf '\nLoad profile is ready with %s web and %s worker replicas.\n' \
  "$LOAD_WEB_REPLICAS" "$LOAD_WORKER_REPLICAS"
