#!/usr/bin/env bash
# Pre-commit hook: lint + test with a wall-clock limit.
# Blocks the commit if formatting, imports, or tests fail.
# Current baseline: ~18s wall-clock. Limit set to 30s for headroom.

echo "Auto-formatting (black + isort)..."
poetry run python -m black --line-length 100 src/ tests/ --quiet
poetry run isort --profile black --line-length 100 src/ tests/ --quiet 2>/dev/null
git add -u

MAX_SECONDS=30

echo "Running tests (${MAX_SECONDS}s limit)..."

timeout "${MAX_SECONDS}" poetry run pytest tests -q --tb=short --timeout=10
exit_code=$?

if [ "$exit_code" -eq 124 ]; then
    echo ""
    echo "BLOCKED: tests exceeded ${MAX_SECONDS}s wall-clock limit."
    echo "Speed up slow tests before committing."
    exit 1
elif [ "$exit_code" -ne 0 ]; then
    echo ""
    echo "BLOCKED: tests failed."
    exit 1
fi
