#!/bin/bash
# Discover modules/*/test/e2e with *.spec.js and run desktop Playwright suite.
# Playwright package: Aurora install-root node_modules (@playwright/test).
# Config / helpers / .env: modules/CoreWebclient/test/e2e/
#
# Usage (from Aurora install root):
#   ./modules/CoreWebclient/test/e2e/run.sh
#   npm run test:e2e-desktop
#   ./modules/CoreWebclient/test/e2e/run.sh -- --setup "MailWebclient Chrome"
#
# Env:
#   SKIP_DEPS_INSTALL=1  (also accepts SKIP_NPM_INSTALL=1 for compatibility)

set -uo pipefail

E2E_DIR="$(cd "$(dirname "$0")" && pwd)"
CORE_DIR="$(cd "$E2E_DIR/../.." && pwd)"
ROOT="$(cd "$E2E_DIR/../../../.." && pwd)"
SKIP_DEPS_INSTALL="${SKIP_DEPS_INSTALL:-${SKIP_NPM_INSTALL:-0}}"

echo "Scanning modules for test/e2e/*.spec.js ..."
echo ""

found=0
for module_dir in "$ROOT"/modules/*; do
    [ -d "$module_dir" ] || continue
    module_name="$(basename "$module_dir")"
    # Skip Core runner folder (shared helpers only, no module scenarios)
    if [ "$module_name" = "CoreWebclient" ]; then
        continue
    fi
    e2e_dir="$module_dir/test/e2e"
    if [ ! -d "$e2e_dir" ]; then
        continue
    fi
    spec_count="$(find "$e2e_dir" -maxdepth 1 -name '*.spec.js' 2>/dev/null | wc -l | tr -d ' ')"
    if [ "$spec_count" = "0" ]; then
        echo "[skip] $module_name — test/e2e has no *.spec.js"
        continue
    fi
    echo "[found] $module_name ($spec_count specs)"
    found=$((found + 1))
done

echo ""
if [ "$found" -eq 0 ]; then
    echo "No modules with test/e2e/*.spec.js — nothing to run."
    exit 1
fi

if [ "$SKIP_DEPS_INSTALL" != "1" ]; then
    if [ ! -d "$ROOT/node_modules/@playwright/test" ]; then
        echo "Installing install-root dependencies (includes Playwright)..."
        (cd "$ROOT" && npm install) || exit 1
    fi
elif [ ! -d "$ROOT/node_modules/@playwright/test" ]; then
    echo "Install-root node_modules/@playwright/test missing (SKIP_DEPS_INSTALL=1)"
    echo "Run from install root: npm install"
    exit 1
fi

echo "----------------------------------------"
echo "Running Playwright (desktop) via CoreWebclient config"
echo "----------------------------------------"

cd "$CORE_DIR"
if [ "${1:-}" = "--" ]; then
    shift
fi
# Invoke run-e2e.js directly — npm run would swallow flags like --setup.
node test/e2e/scripts/run-e2e.js "$@"
