#!/bin/bash

# Run all checks that CI runs locally
# Useful for testing before pushing to ensure CI will pass

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_header() {
    echo -e "${BLUE}$1${NC}"
    echo "$(echo "$1" | sed 's/./=/g')"
}

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

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

echo "🚀 Running all CI checks locally..."
echo ""

# Format check
print_header "📝 Format Check"
if cargo fmt --all -- --check; then
    print_success "Code formatting is correct"
else
    print_error "Code formatting issues found"
    echo "Run 'cargo fmt --all' to fix formatting"
    exit 1
fi
echo ""

# Clippy
print_header "🔧 Clippy"
if cargo clippy --all-targets --all-features -- -D warnings; then
    print_success "Clippy checks passed"
else
    print_error "Clippy found issues"
    exit 1
fi
echo ""

# Build
print_header "🔨 Build"
if cargo build --all-features --verbose; then
    print_success "Build successful"
else
    print_error "Build failed"
    exit 1
fi
echo ""

# Build examples
print_header "📚 Build Examples"
if cargo build --examples --all-features --verbose; then
    print_success "Examples build successful"
else
    print_error "Examples build failed"
    exit 1
fi
echo ""

# Tests
print_header "🧪 Tests"
if cargo test --all-features --verbose; then
    print_success "All tests passed"
else
    print_error "Some tests failed"
    exit 1
fi
echo ""

# Documentation
print_header "📖 Documentation"
if cargo doc --all-features --no-deps --document-private-items; then
    print_success "Documentation built successfully"
else
    print_error "Documentation build failed"
    exit 1
fi
echo ""

print_success "🎉 All CI checks passed! Your code is ready to push." 