#!/bin/bash
#
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# Pre-commit hook for bunnylol.rs
# Runs formatting, tests, and checks before allowing commit

set -e

echo "Running pre-commit checks..."
echo ""

# Format code automatically (moved to first for best UX)
echo "🎨 Formatting code..."
cargo fmt --all

# Re-add any Rust files that were modified by formatting
FORMATTED_FILES=$(git diff --name-only | grep '\.rs$' || true)
if [ -n "$FORMATTED_FILES" ]; then
    echo "$FORMATTED_FILES" | xargs git add
    FILE_COUNT=$(echo "$FORMATTED_FILES" | wc -l | tr -d ' ')
    echo "✅ Formatted and re-staged $FILE_COUNT file(s)"
else
    echo "✅ No files needed formatting"
fi

# Run cargo check (fast compilation check)
echo ""
echo "🔍 Running cargo check..."
cargo check --all-features

# Run clippy (linter)
echo ""
echo "🔧 Running cargo clippy..."
cargo clippy --all-features -- -D warnings

# Run tests
echo ""
echo "🧪 Running cargo test..."
cargo test --all-features

echo ""
echo "✅ All checks passed! Proceeding with commit."
