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

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_DIR
readonly INSTALLED_TRANSPORT='/home/user/.claude/bin/e2e-remote'

transport="${INSTALLED_TRANSPORT}"
if [[ "${IPZ_RUN_REMOTE_TEST_MODE:-0}" == 1 ]]; then
    transport="${IPZ_E2E_REMOTE_BIN:-}"
fi

[[ -x "${transport}" ]] || {
    printf 'E2E transport is missing or not executable: %s\n' "${transport}" >&2
    exit 1
}
[[ -x "${SCRIPT_DIR}/remote-stack.sh" ]] || {
    printf 'Remote stack launcher is missing or not executable: %s\n' "${SCRIPT_DIR}/remote-stack.sh" >&2
    exit 1
}
[[ -x "${SCRIPT_DIR}/run-remote-client.sh" ]] || {
    printf 'Remote client launcher is missing or not executable: %s\n' "${SCRIPT_DIR}/run-remote-client.sh" >&2
    exit 1
}
[[ -x "${SCRIPT_DIR}/run-playwright.sh" ]] || {
    printf 'Playwright launcher is missing or not executable: %s\n' "${SCRIPT_DIR}/run-playwright.sh" >&2
    exit 1
}

cd "${SCRIPT_DIR}"
exec "${transport}" \
    --server './remote-stack.sh' \
    --wait-port 8080 \
    --wait-sec 600 \
    --env 'WP_BASE_URL=http://127.0.0.1:8080' \
    --mkdir '../../tests/artifacts/universal-admin' \
    --mkdir '../../tests/artifacts/universal-admin/calibration/run-1' \
    --mkdir '../../tests/artifacts/universal-admin/calibration/run-2' \
    --mkdir '../../tests/artifacts/universal-admin/calibration/run-3' \
    -- './run-remote-client.sh'
