#!/bin/bash
# Pre-commit hook for VerySmolCode
# Runs static analysis and tests before committing

set -e

# Source cargo env
if [ -f "$HOME/.cargo/env" ]; then
    source "$HOME/.cargo/env"
fi

echo "Running pre-commit checks..."

# 1. Format check
echo "  [1/3] Checking formatting..."
cargo fmt -- --check 2>/dev/null || {
    echo "FAIL: Code is not formatted. Run 'cargo fmt' first."
    exit 1
}

# 2. Clippy (strict)
echo "  [2/3] Running clippy..."
cargo clippy -- -D warnings 2>&1 | tail -5 || {
    echo "FAIL: Clippy found issues."
    exit 1
}

# 3. Tests
echo "  [3/3] Running tests..."
cargo test 2>&1 | tail -5 || {
    echo "FAIL: Tests failed."
    exit 1
}

echo "All pre-commit checks passed!"
