#!/bin/bash
# Pre-commit hook to run checks matching GitHub CI
#
# To install this hook, run: ./scripts/setup-hooks.sh
# Or manually: cp hooks/pre-commit .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit

set -e

echo "Running pre-commit checks..."

# 1. Format check
echo "🔍 Checking code formatting..."
if ! cargo fmt --check; then
    echo "❌ Format check failed. Run 'cargo fmt' to fix."
    exit 1
fi
echo "✅ Format check passed"

# 2. Clippy lints
echo "🔍 Running clippy..."
if ! cargo clippy --all-targets --all-features -- -D warnings; then
    echo "❌ Clippy found issues. Fix them before committing."
    exit 1
fi
echo "✅ Clippy passed"

# 3. Build check
echo "🔍 Checking build..."
if ! cargo check --all-features; then
    echo "❌ Build check failed."
    exit 1
fi
echo "✅ Build check passed"

# 4. Dependency check
echo "🔍 Checking dependencies with cargo-deny..."
if ! command -v cargo-deny &> /dev/null; then
    echo "⚠️  cargo-deny not installed. Run: cargo install cargo-deny"
    echo "⚠️  Skipping dependency check..."
else
    if ! cargo deny check; then
        echo "❌ Dependency check failed."
        exit 1
    fi
    echo "✅ Dependency check passed"
fi

# 5. Run library tests
echo "🔍 Running library tests..."
if ! cargo test --lib --all-features; then
    echo "❌ Library tests failed."
    exit 1
fi
echo "✅ Library tests passed"

# 6. Run integration tests (tests/ folder)
echo "🔍 Running integration tests..."
if ! cargo test --tests --all-features; then
    echo "❌ Integration tests failed."
    exit 1
fi
echo "✅ Integration tests passed"

# 7. Run doctests
echo "🔍 Running doctests..."
if ! cargo test --doc --all-features; then
    echo "❌ Doctests failed."
    exit 1
fi
echo "✅ Doctests passed"

echo "✅ All pre-commit checks passed!"
