#!/bin/sh
# Pre-commit hook for rustfmt, clippy, and file size checks

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 "Error: Not in a git repository"
    exit 1
fi

# Get the git hooks directory
HOOKS_DIR=$(git rev-parse --git-dir)/hooks
if [ -f "$(git rev-parse --show-toplevel)/.githooks/check-file-size.sh" ]; then
    HOOKS_DIR="$(git rev-parse --show-toplevel)/.githooks"
fi

# Check file sizes
if [ -f "$HOOKS_DIR/check-file-size.sh" ]; then
    if ! "$HOOKS_DIR/check-file-size.sh"; then
        exit 1
    fi
else
    echo "Warning: File size check script not found"
fi

# Run rustfmt check
echo "Checking code formatting with rustfmt..."
if ! cargo fmt -- --check; then
    echo "Error: Code formatting issues detected!"
    echo "Please run 'cargo fmt' to fix formatting issues."
    exit 1
fi

# Run clippy
echo "Running clippy lints..."
if ! cargo clippy -- -D warnings; then
    echo "Error: Clippy warnings detected!"
    echo "Please fix the clippy warnings before committing."
    exit 1
fi

# Run tests (optional - can be slow)
# Uncomment the following to run tests on every commit
# echo "Running tests..."
# if ! cargo test; then
#     echo "Error: Tests failed!"
#     echo "Please fix the failing tests before committing."
#     exit 1
# fi

# Check shell scripts if shellcheck is available
if command -v shellcheck >/dev/null 2>&1; then
    echo "Checking shell scripts with shellcheck..."
    SHELL_SCRIPTS=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(sh|bash)$' || true)
    if [ -n "$SHELL_SCRIPTS" ]; then
        FAILED=0
        for script in $SHELL_SCRIPTS; do
            if [ -f "$script" ]; then
                if ! shellcheck "$script"; then
                    FAILED=1
                fi
            fi
        done
        if [ $FAILED -ne 0 ]; then
            echo "Error: Shell script issues detected!"
            echo "Please fix the shellcheck warnings before committing."
            exit 1
        fi
    fi
    
    # Also check hook scripts themselves if they're being committed
    HOOK_SCRIPTS=$(git diff --cached --name-only --diff-filter=ACM | grep -E '^\.githooks/' | grep -v '\.md$' || true)
    if [ -n "$HOOK_SCRIPTS" ]; then
        FAILED=0
        for script in $HOOK_SCRIPTS; do
            if [ -f "$script" ] && [ -x "$script" ]; then
                if ! shellcheck "$script"; then
                    FAILED=1
                fi
            fi
        done
        if [ $FAILED -ne 0 ]; then
            echo "Error: Hook script issues detected!"
            echo "Please fix the shellcheck warnings before committing."
            exit 1
        fi
    fi
else
    # Only warn if shell scripts are being committed
    SHELL_SCRIPTS=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(sh|bash)$|^\.githooks/[^.]+$' | grep -v '\.md$' || true)
    if [ -n "$SHELL_SCRIPTS" ]; then
        echo "Warning: shellcheck not installed - shell scripts not linted"
        echo "Install with: brew install shellcheck (macOS) or apt-get install shellcheck (Linux)"
    fi
fi

echo "All pre-commit checks passed!"
exit 0