#!/usr/bin/env bash

set -euo pipefail

if [[ $# -lt 1 ]]; then
  echo "usage: $0 <base-url>" >&2
  echo "example: $0 https://rmcp.itexp.me" >&2
  exit 64
fi

if [[ -z "${CRANK_STAGING_ADMIN_EMAIL:-}" ]]; then
  echo "CRANK_STAGING_ADMIN_EMAIL is required" >&2
  exit 64
fi

if [[ -z "${CRANK_STAGING_ADMIN_PASSWORD:-}" ]]; then
  echo "CRANK_STAGING_ADMIN_PASSWORD is required" >&2
  exit 64
fi

BASE_URL="${1%/}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"

echo "authenticated staging smoke: ${BASE_URL}"

cd "${REPO_ROOT}/apps/ui"
PLAYWRIGHT_BASE_URL="${BASE_URL}" \
PLAYWRIGHT_SKIP_WEB_SERVER=1 \
CRANK_E2E_ADMIN_EMAIL="${CRANK_STAGING_ADMIN_EMAIL}" \
CRANK_E2E_ADMIN_PASSWORD="${CRANK_STAGING_ADMIN_PASSWORD}" \
npx playwright test tests/e2e/staging-authenticated.spec.js
