#!/bin/bash

# Script to set up git hooks for the Rust project

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

print_info() {
    echo -e "${BLUE}ℹ️  $1${NC}"
}

print_success() {
    echo -e "${GREEN}✅ $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}⚠️  $1${NC}"
}

print_error() {
    echo -e "${RED}❌ $1${NC}"
}

echo "🔧 Git Hooks Setup for Rust Project"
echo "===================================="
echo ""

if [ ! -d ".git" ]; then
    print_error "This is not a git repository. Please run this script from the root of your git repository."
    exit 1
fi

echo "Available hook configurations:"
echo "1. Full checks (formatting, clippy, build, tests) - Recommended for releases"
echo "2. Light checks (formatting, clippy only) - Faster for development"
echo "3. Format only (formatting only) - Fastest option"
echo "4. Disable hooks"
echo ""

read -p "Choose an option (1-4): " choice

case $choice in
    1)
        print_info "Setting up full pre-commit checks..."
        cp .git/hooks/pre-commit-light .git/hooks/pre-commit-backup 2>/dev/null || true
        # The full pre-commit hook is already in place
        print_success "Full pre-commit checks enabled"
        print_warning "Note: This will run all tests before each commit (slower but thorough)"
        ;;
    2)
        print_info "Setting up lightweight pre-commit checks..."
        cp .git/hooks/pre-commit .git/hooks/pre-commit-full 2>/dev/null || true
        cp .git/hooks/pre-commit-light .git/hooks/pre-commit
        print_success "Lightweight pre-commit checks enabled"
        print_info "This will run formatting and clippy checks only"
        ;;
    3)
        print_info "Setting up format-only checks..."
        cat > .git/hooks/pre-commit << 'EOF'
#!/bin/bash
set -e
echo "🎨 Running code formatting..."
cargo fmt --all
git add .
echo "✅ Code formatted successfully"
EOF
        chmod +x .git/hooks/pre-commit
        print_success "Format-only pre-commit hook enabled"
        print_info "This will only format code before commits"
        ;;
    4)
        print_info "Disabling pre-commit hooks..."
        mv .git/hooks/pre-commit .git/hooks/pre-commit-disabled 2>/dev/null || true
        print_success "Pre-commit hooks disabled"
        print_warning "Remember to run 'cargo fmt' and 'cargo clippy' manually"
        ;;
    *)
        print_error "Invalid option. Please choose 1-4."
        exit 1
        ;;
esac

echo ""
print_info "Hook setup complete!"
echo ""
print_info "Useful commands:"
echo "  • Test current formatting: cargo fmt --all -- --check"
echo "  • Fix formatting: cargo fmt --all"
echo "  • Run clippy: cargo clippy --all-targets --all-features"
echo "  • Run tests: cargo test --all-features"
echo "  • Skip hooks for one commit: git commit --no-verify"
echo "" 