#!/usr/bin/env bash
# pre-commit: run ruff check (when installed) and the test suite when a commit touches Python under src/, scripts/, tools/ or tests/;
# a commit that touches README.md, docs/architecture.md, docs/workflow.md, scripts/README.md or
# CONTRIBUTING.md and no Python runs the README tests alone.
# Enable once per clone:  git config core.hooksPath .githooks
# Tests import this checkout's src/ (PYTHONPATH), so a worktree tests its own code.
set -euo pipefail

STAGED=$(git diff --cached --name-only --diff-filter=ACMRD)
TESTS="tests/"
if ! grep -qE '^(src|scripts|tools|tests)/.*\.py$' <<<"$STAGED"; then
    grep -qxE 'README\.md|docs/architecture\.md|docs/workflow\.md|scripts/README\.md|CONTRIBUTING\.md' <<<"$STAGED" || exit 0
    TESTS="tests/copper/test_readme_names.py tests/copper/test_readme_links.py tests/test_public_surface.py tests/test_import_direction.py"
fi

PY=${PYSUTO_PYTHON:-python}
export PYTHONPATH="src${PYTHONPATH:+:$PYTHONPATH}"
if ! "$PY" -c "import pysuto" 2>/dev/null; then
    echo "pre-commit: '$PY' cannot import pysuto; activate the conda env 'pysuto' or set PYSUTO_PYTHON." >&2
    exit 1
fi
if command -v ruff >/dev/null 2>&1; then
    ruff check
else
    echo "pre-commit: ruff not installed; CI lints"
fi
# shellcheck disable=SC2086  # TESTS is a list of paths
exec "$PY" -m pytest $TESTS -q -x
