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

job_name="${1:-adminfront-tests}"
repo_root="$(pwd)"
tmp_dir=""

cleanup() {
  if [ -n "${tmp_dir:-}" ] && [ -d "$tmp_dir" ]; then
    rm -rf "$tmp_dir" || true
  fi
}

trap "cleanup; exit" INT TERM
trap "cleanup" EXIT

mkdir -p reports

tmp_dir="$(mktemp -d /tmp/baron-sso-adminfront-tests.XXXXXX)"
pnpm_store_dir="$tmp_dir/pnpm-store"
# [Fix] Use a relative path for the build output directory
export ADMINFRONT_BUILD_OUT_DIR="dist"

seed_dir=""
for candidate in \
  /tmp/baron-sso-adminfront-tests.FRPGmL \
  /tmp/baron-sso-adminfront-tests.mumSD6 \
  /tmp/baron-sso-adminfront-tests.pwAMAt; do
  if [ -d "$candidate/adminfront/node_modules" ] && \
    [ -d "$candidate/common/node_modules" ]; then
    seed_dir="$candidate"
    break
  fi
done
if [ -z "$seed_dir" ]; then
  for candidate in /tmp/baron-sso-adminfront-tests.*; do
    if [ "$candidate" != "$tmp_dir" ] && \
      [ -d "$candidate/adminfront/node_modules" ] && \
      [ -d "$candidate/common/node_modules" ]; then
      seed_dir="$candidate"
      break
    fi
  done
fi
reuse_seed_node_modules=0
mkdir -p "$tmp_dir/scripts"
cp "$repo_root/scripts/playwrightHostDeps.cjs" "$tmp_dir/scripts/"

if command -v rsync >/dev/null 2>&1; then
  rsync -rlptD --delete \
    --exclude 'node_modules' \
    --exclude 'playwright-report' \
    --exclude 'test-results' \
    "$repo_root/adminfront/" "$tmp_dir/adminfront/"
  rsync -rlptD --delete \
    --exclude 'node_modules' \
    "$repo_root/common/" "$tmp_dir/common/"
  rm -rf "$tmp_dir/common/node_modules"
else
  cp -R "$repo_root/adminfront" "$tmp_dir/adminfront"
  cp -R "$repo_root/common" "$tmp_dir/common"
  rm -rf "$tmp_dir/adminfront/node_modules" \
    "$tmp_dir/common/node_modules" \
    "$tmp_dir/adminfront/playwright-report" \
    "$tmp_dir/adminfront/test-results"
fi

if [ -n "$seed_dir" ] && [ "$seed_dir" != "$tmp_dir" ] && \
  [ -d "$seed_dir/adminfront/node_modules" ] && \
  [ -d "$seed_dir/common/node_modules" ]; then
  cp -a "$seed_dir/adminfront/node_modules" "$tmp_dir/adminfront/"
  cp -a "$seed_dir/common/node_modules" "$tmp_dir/common/"
  reuse_seed_node_modules=1
fi

if [ ! -d "$tmp_dir/adminfront/node_modules" ] || \
  [ ! -d "$tmp_dir/common/node_modules" ]; then
  rm -rf "$tmp_dir/adminfront/playwright-report" \
    "$tmp_dir/adminfront/test-results"
fi

is_port_available() {
  local port="$1"
  node -e '
    const net = require("net");
    const port = Number(process.argv[1]);
    const server = net.createServer();
    server.once("error", () => process.exit(1));
    server.once("listening", () => server.close(() => process.exit(0)));
    server.listen(port, "127.0.0.1");
  ' "$port"
}

find_available_port() {
  node -e '
    const net = require("net");
    const server = net.createServer();
    server.listen(0, "127.0.0.1", () => {
      const address = server.address();
      process.stdout.write(String(address.port));
      server.close();
    });
  '
}

run_with_retry() {
  local max_attempts="$1"
  shift

  local attempt=1
  local exit_code=0
  while [ "$attempt" -le "$max_attempts" ]; do
    if "$@"; then
      return 0
    fi

    exit_code=$?
    if [ "$attempt" -ge "$max_attempts" ]; then
      return "$exit_code"
    fi

    echo "==> command failed (attempt $attempt/$max_attempts): $*"
    echo "==> retrying in 10 seconds..."
    sleep 10
    attempt=$((attempt + 1))
  done

  return "$exit_code"
}

playwright_install_cmd=(pnpm exec playwright install)
playwright_install_desc="pnpm exec playwright install"
playwright_project_args=()

has_webkit_host_dependencies() {
  if [ "$(uname -s)" != "Linux" ]; then
    return 0
  fi
  if ! command -v ldconfig >/dev/null 2>&1; then
    return 1
  fi

  local missing=0
  local lib
  for lib in \
    libgtk-4.so.1 \
    libgraphene-1.0.so.0 \
    libxslt.so.1 \
    libevent-2.1.so.7 \
    libopus.so.0 \
    libgstallocators-1.0.so.0 \
    libgstapp-1.0.so.0 \
    libgstpbutils-1.0.so.0 \
    libgstaudio-1.0.so.0 \
    libgsttag-1.0.so.0 \
    libgstvideo-1.0.so.0 \
    libgstgl-1.0.so.0 \
    libgstcodecparsers-1.0.so.0 \
    libgstfft-1.0.so.0 \
    libflite.so.1 \
    libwebpdemux.so.2 \
    libavif.so.16 \
    libharfbuzz-icu.so.0 \
    libwebpmux.so.3 \
    libwayland-server.so.0 \
    libmanette-0.2.so.0 \
    libenchant-2.so.2 \
    libhyphen.so.0 \
    libsecret-1.so.0 \
    libwoff2dec.so.1.0.2 \
    libx264.so; do
    if ! ldconfig -p 2>/dev/null | grep -Fq "$lib"; then
      missing=1
      break
    fi
  done

  [ "$missing" -eq 0 ]
}

if [ "$(id -u)" -eq 0 ]; then
  playwright_install_cmd=(pnpm exec playwright install --with-deps)
  playwright_install_desc="pnpm exec playwright install --with-deps"
elif command -v sudo >/dev/null 2>&1 && sudo -n true >/dev/null 2>&1; then
  playwright_install_cmd=(pnpm exec playwright install --with-deps)
  playwright_install_desc="pnpm exec playwright install --with-deps"
elif ! has_webkit_host_dependencies; then
  playwright_install_cmd=(pnpm exec playwright install chromium firefox)
  playwright_install_desc="pnpm exec playwright install chromium firefox"
  playwright_project_args=(--project=chromium --project=firefox)
  {
    echo "# Adminfront WebKit Skipped"
    echo
    echo "- Reason: WebKit host dependencies are not installed and this user cannot run passwordless sudo."
    echo "- Action: Running Chromium and Firefox projects only."
    echo "- To enable WebKit locally: run \`cd adminfront && pnpm exec playwright install-deps webkit\` with sudo privileges."
  } > reports/adminfront-webkit-skipped.md
fi

set +e
(
  cd "$tmp_dir/common"
  echo "packages: ['.', '../adminfront']" > pnpm-workspace.yaml

  if [ "$reuse_seed_node_modules" -eq 0 ]; then
    corepack enable
    corepack prepare pnpm@10.5.2 --activate
    run_with_retry 3 env CI=true pnpm install --no-frozen-lockfile --shamefully-hoist --store-dir "$pnpm_store_dir"
  fi

  cd "$tmp_dir/adminfront"
  run_with_retry 3 env CI=true pnpm install --no-frozen-lockfile --shamefully-hoist --store-dir "$pnpm_store_dir"
) 2>&1 | tee reports/adminfront-install.log
install_exit_code=${STATUS:-$?}
set -e

if [ "$install_exit_code" -ne 0 ]; then
  {
    echo "# Adminfront Test Failure Report"
    echo
    echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
    echo "- Job: \`${job_name}\`"
    echo "- Reason: \`Dependency install failed\`"
    echo "- Exit Code: \`$install_exit_code\`"
    echo
    echo "## Command"
    echo "\`cd common && echo \"packages: ['.', '../adminfront']\" > pnpm-workspace.yaml && env CI=true pnpm install --no-frozen-lockfile --shamefully-hoist\`"
    echo
    echo "## Install Log Tail (last 200 lines)"
    echo '```text'
    tail -n 200 reports/adminfront-install.log
    echo '```'
  } > reports/adminfront-test-failure-report.md
  exit 1
fi

# [Fix] Explicitly build to 'dist' and ensure it's available for 'vite preview'.
(
  cd "$tmp_dir/adminfront"
  env CI=true ADMINFRONT_BUILD_OUT_DIR="dist" pnpm run build
) 2>&1 | tee reports/adminfront-build.log
build_exit_code=${PIPESTATUS[0]}
if [ "$build_exit_code" -ne 0 ]; then
  {
    echo "# Adminfront Test Failure Report"
    echo
    echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
    echo "- Job: \`${job_name}\`"
    echo "- Reason: \`Build failed\`"
    echo "- Exit Code: \`$build_exit_code\`"
    echo
    echo "## Command"
    echo "\`cd adminfront && env CI=true pnpm run build\`"
    echo
    echo "## Build Log Tail (last 200 lines)"
    echo '```text'
    tail -n 200 reports/adminfront-build.log
    echo '```'
  } > reports/adminfront-test-failure-report.md
  exit 1
fi

set +e
(
  cd "$tmp_dir/adminfront"
  "${playwright_install_cmd[@]}"
) 2>&1 | tee reports/adminfront-provision.log
provision_exit_code=${PIPESTATUS[0]}
set -e

if [ "$provision_exit_code" -ne 0 ]; then
  {
    echo "# Adminfront Test Failure Report"
    echo
    echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
    echo "- Job: \`${job_name}\`"
    echo "- Reason: \`Browser provisioning failed\`"
    echo "- Exit Code: \`$provision_exit_code\`"
    echo
    echo "## Command"
    echo "\`cd adminfront && ${playwright_install_desc}\`"
    echo "## Provision Log Tail (last 200 lines)"
    echo '```text'
    tail -n 200 reports/adminfront-provision.log
    echo '```'
  } > reports/adminfront-test-failure-report.md
  exit 1
fi

set +e
port="${PORT:-5180}"
if ! is_port_available "$port"; then
  fallback_port="$(find_available_port)"
  echo "==> requested PORT=$port is already in use; switching to PORT=$fallback_port"
  port="$fallback_port"
fi
echo "==> adminfront using PORT=$port"
(
  cd "$tmp_dir/adminfront"
  CI=true PORT="$port" PLAYWRIGHT_WORKERS="${PLAYWRIGHT_WORKERS:-1}" \
    pnpm exec playwright test --max-failures=1 "${playwright_project_args[@]}"
) 2>&1 | tee reports/adminfront-test.log
test_exit_code=${PIPESTATUS[0]}
set -e

[ -d "$tmp_dir/adminfront/playwright-report" ] && rm -rf reports/adminfront-playwright-report && cp -R "$tmp_dir/adminfront/playwright-report" reports/adminfront-playwright-report || true
[ -d "$tmp_dir/adminfront/test-results" ] && rm -rf reports/adminfront-test-results && cp -R "$tmp_dir/adminfront/test-results" reports/adminfront-test-results || true

if [ "$test_exit_code" -ne 0 ]; then
  {
    echo "# Adminfront Test Failure Report"
    echo
    echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
    echo "- Job: \`${job_name}\`"
    echo "- Exit Code: \`$test_exit_code\`"
    echo
    echo "## Commands"
    echo "1. \`cd common && echo \"packages: ['.', '../adminfront']\" > pnpm-workspace.yaml && env CI=true pnpm install --no-frozen-lockfile --shamefully-hoist\`"
    echo "2. \`cd adminfront && env CI=true pnpm run build\`"
    echo "3. \`${playwright_install_desc}\`"
    echo "4. \`pnpm exec playwright test\`"
    echo
    echo "## Log Tail (last 200 lines)"
    echo '```text'
    tail -n 200 reports/adminfront-test.log
    echo '```'
  } > reports/adminfront-test-failure-report.md
fi

exit "$test_exit_code"
