#!/usr/bin/env bash
# Pre-commit hook for Maple Proxy
# Ensures code quality before commits

set -e

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

# Check if we're in a git repository
if ! git rev-parse --git-dir >/dev/null 2>&1; then
    echo "❌ Not in a git repository"
    exit 1
fi

# Function to print step status
print_step() {
    echo "📋 $1..."
}

print_success() {
    echo "✅ $1"
}

print_error() {
    echo "❌ $1"
}

# Rust formatting check
print_step "Checking Rust formatting"
if cargo fmt --check; then
    print_success "Code formatting is correct"
else
    print_error "Code formatting issues found"
    echo "💡 Run 'cargo fmt' to fix formatting"
    exit 1
fi

# Clippy linting
print_step "Running Clippy lints"
if cargo clippy --all-targets --all-features -- -D warnings; then
    print_success "Clippy checks passed"
else
    print_error "Clippy lints failed"
    echo "💡 Fix the issues above before committing"
    exit 1
fi

# Cargo check (compilation)
print_step "Checking compilation"
if cargo check --all-targets --all-features; then
    print_success "Code compiles successfully"
else
    print_error "Compilation failed"
    exit 1
fi

# Run tests
print_step "Running tests"
if cargo test --all-features; then
    print_success "All tests passed"
else
    print_error "Tests failed"
    exit 1
fi

echo ""
echo "🎉 All pre-commit checks passed!"
echo "   Ready to commit"