#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

pick_free_port() {
  node scripts/pick-port.js "$1"
}

run_vitest_suite() {
  preferred_port="$1"
  shift
  vitest_port="$(pick_free_port "$preferred_port")" || return 1
  NEOTOMA_HTTP_PORT="$vitest_port" HTTP_PORT="$vitest_port" "$@"
}

run_playwright_suite() {
  vitest_port="$(pick_free_port 18080)" || return 1
  ui_port="$(pick_free_port 5195)" || return 1
  NEOTOMA_HTTP_PORT="$vitest_port" \
  HTTP_PORT="$vitest_port" \
  PLAYWRIGHT_UI_BASE_URL="http://127.0.0.1:$ui_port" \
  "$@"
}

# Type check (MANDATORY - catches type errors before tests)
echo "Running type check..."
npm run type-check || exit 1

# Lint check (MANDATORY - catches code style issues)
echo "Running linter..."
npm run lint || exit 1

# Format check (MANDATORY - rejects unformatted code; run `npm run format` to fix)
echo "Running format check..."
npm run format:check || {
  echo ""
  echo "❌ Prettier formatting drift detected. Fix with: npm run format"
  exit 1
}

echo "Running site copy style lint (writing_style_guide)..."
npm run lint:site-copy || exit 1

if [ "$SKIP_TESTS" = "1" ] || [ "$NEOTOMA_SKIP_TESTS" = "1" ]; then
  echo "Skipping tests (SKIP_TESTS=1 or NEOTOMA_SKIP_TESTS=1)"
else
  # Unit tests (MANDATORY - catches logic errors)
  echo "Running unit tests..."
  run_vitest_suite 18080 npm test || exit 1

  # Integration tests (if backend/DB files changed)
  if git diff --cached --name-only | grep -E "(src/|tests/integration)" > /dev/null; then
    echo "Running integration tests..."
    run_vitest_suite 18120 npm run test:integration || exit 1
  fi

  # E2E tests (if frontend/UI files changed)
  if git diff --cached --name-only | grep -E "(frontend/|playwright/)" > /dev/null; then
    echo "Running E2E tests..."
    run_playwright_suite npm run test:e2e || exit 1
  fi

  # Validate Playwright coverage map
  npm run validate:coverage

  # Check Playwright test coverage for UI changes
  npm run check:pw-coverage
fi

# Validate documentation dependencies for modified docs
npm run validate:doc-deps

# Sync .cursor/ and .claude/ if rule/command sources modified
./scripts/linters/sync_agent_instructions_pre_commit.sh
