#!/bin/sh

# Check if any Rust files are being committed
RUST_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.rs$')

if [ -z "$RUST_FILES" ]; then
    echo "No Rust files in this commit, skipping checks."
    exit 0
fi

echo "Rust files detected in commit, running checks..."
echo ""

# Run cargo fmt check before committing (using nightly for rustfmt.toml features)
echo "Running cargo +nightly fmt --check..."
cargo +nightly fmt --check

if [ $? -ne 0 ]; then
    echo ""
    echo "❌ Code formatting check failed!"
    echo "Please run 'cargo +nightly fmt' to format your code before committing."
    exit 1
fi

echo "✅ Code formatting check passed"

# Run clippy on the affected package
# Note: Clippy needs to check the whole crate for proper analysis
echo ""
echo "Running cargo clippy..."
cargo clippy --all-targets --all-features -- -D warnings

if [ $? -ne 0 ]; then
    echo ""
    echo "❌ Clippy check failed!"
    echo "Please fix the clippy warnings before committing."
    exit 1
fi

echo "✅ Clippy check passed"
echo ""
echo "All pre-commit checks passed! ✨"

