#!/bin/sh
#
# Pre-commit hook that runs tests to prevent regressions
#
# To install this hook, run:
#   cp scripts/pre-commit .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit
#
# Or use: ./scripts/install-hooks.sh
#

set -e

echo "Checking formatting..."
unformatted=$(gofmt -l .)
if [ -n "$unformatted" ]; then
    echo ""
    echo "❌ These files are not gofmt-clean:"
    echo "$unformatted"
    echo "Run: gofmt -w ."
    exit 1
fi

echo "Running go vet..."
if ! go vet ./...; then
    echo "❌ go vet failed. Commit aborted."
    exit 1
fi

echo "Running tests before commit..."

# Run all tests with -short flag to skip long-running tests
if ! go test ./... -short; then
    echo ""
    echo "❌ Tests failed. Commit aborted."
    echo "Fix the failing tests before committing."
    echo ""
    echo "To skip this check (not recommended), use:"
    echo "  git commit --no-verify"
    exit 1
fi

echo "✅ All tests passed!"
exit 0
