#!/usr/bin/env bash
# Pre-commit hook: fast quality checks on staged Go files
set -e

echo "🔍 Pre-commit checks..."

# Check if any Go files are staged
STAGED_GO_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$' || true)

if [ -z "$STAGED_GO_FILES" ]; then
    echo "✅ No Go files staged, skipping checks"
    exit 0
fi

# 1. go vet
echo "  Running go vet..."
CGO_ENABLED=0 go vet ./... || { echo "❌ go vet failed"; exit 1; }

# 2. golangci-lint (skip if not installed)
if command -v golangci-lint &> /dev/null; then
    echo "  Running golangci-lint..."
    golangci-lint run --new-from-rev=HEAD~1 ./... || { echo "❌ golangci-lint failed"; exit 1; }
else
    echo "  ⚠️  golangci-lint not found, skipping (install: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest)"
fi

# 3. Fast unit tests (short mode)
echo "  Running fast tests..."
CGO_ENABLED=0 go test -short -count=1 ./... || { echo "❌ tests failed"; exit 1; }

echo "✅ All pre-commit checks passed"
