#!/bin/bash
#
# Pre-commit hook for lex-core
# Runs formatting, linting, and testing
#
# Usage:
#   ./scripts/pre-commit           # Run on staged files (git hook mode)
#   ./scripts/pre-commit --all     # Run on all files (manual mode)

set -e

echo "Running pre-commit checks for lex-core..."

# 1. Check formatting
echo ""
echo "Checking code formatting..."
if ! cargo fmt -- --check; then
    echo "Formatting issues found. Run 'cargo fmt' to fix."
    exit 1
fi
echo "Formatting OK"

# 2. Run Clippy
echo ""
echo "Running Clippy..."
if ! cargo clippy --all-targets --all-features -- -D warnings; then
    echo "Clippy found issues!"
    exit 1
fi
echo "Clippy OK"

# 3. Build
echo ""
echo "Building..."
if ! cargo build; then
    echo "Build failed!"
    exit 1
fi
echo "Build OK"

# 4. Run tests
echo ""
echo "Running tests..."
if command -v cargo-nextest >/dev/null 2>&1; then
    if ! cargo nextest run; then
        echo "Tests failed!"
        exit 1
    fi
else
    if ! cargo test; then
        echo "Tests failed!"
        exit 1
    fi
fi
echo "Tests OK"

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