#!/bin/sh
# Opt-in local gate (GitHub Actions is paused): git config core.hooksPath .githooks
# THE one local gate (a duplicate .pre-commit-config.yaml with auto-fixers was removed; to auto-fix instead of
# check: `uv run ruff check --fix && uv run ruff format`).
# Fast: lint + format check on staged Python, tests that import the staged modules, a scheduler/finalize smoke set.
# F-289: eslint on staged static/js when node_modules is present.
# F-291: docs-drift when cli.py / mcp_server.py staged.
# Skip once with `git commit --no-verify`; the full gate is still `pwsh scripts/ci.ps1`.
set -e

js=$(git diff --cached --name-only --diff-filter=ACMR -- 'tqdaemon/static/js/*.js' 'tqdaemon/static/js/**/*.js')
if [ -n "$js" ] && [ -d node_modules ]; then
  # shellcheck disable=SC2086
  npx eslint $js
fi

py=$(git diff --cached --name-only --diff-filter=ACMR -- '*.py')
[ -z "$py" ] && [ -z "$js" ] && exit 0
[ -z "$py" ] && exit 0

# The CLI / MCP surface must stay documented (docs/surface_map.json, docs/mcp.md).
if echo "$py" | grep -q -E '(^|/)(cli|mcp_server)\.py$'; then
  uv run python scripts/check_docs.py
fi

# shellcheck disable=SC2086
uv run ruff check $py
# shellcheck disable=SC2086
uv run ruff format --check $py
# Tests that import the staged modules (scripts/select_tests.py), plus a fixed scheduler/finalize smoke set.
# shellcheck disable=SC2086
related=$(uv run python scripts/select_tests.py $py)
# shellcheck disable=SC2086
uv run pytest -q -x -n auto -m "not slow and not stress and not nightly and not flaky" \
  tests/test_ratchets.py \
  tests/test_caches_registry.py \
  tests/test_daemon_tick.py \
  tests/test_daemon_tick_helpers.py \
  tests/test_decide_terminal_status.py \
  tests/test_get_prefix_routes.py \
  tests/test_cap_false_completed.py \
  $related
