#!/usr/bin/env bash

# Local CI verification script
# Run this before pushing to catch errors early

set -e

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

# Helper functions
print_step() {
    echo -e "${BLUE}▶${NC} $1"
}

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

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

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

# Main script
echo ""
echo "========================================="
echo "  pot-head - Local CI Verification"
echo "========================================="
echo ""

# 1. Format check
print_step "Checking code formatting..."
cargo fmt --all -- --check
print_success "Code formatting OK (main library)"

# Check examples formatting
if [ -d "examples/filtering" ]; then
    print_step "  Checking filtering example formatting..."
    (cd examples/filtering && cargo fmt -- --check)
    print_success "  filtering formatting OK"
else
    print_warning "  filtering example not found, skipping"
fi

if [ -d "examples/interactive" ]; then
    print_step "  Checking interactive example formatting..."
    (cd examples/interactive && cargo fmt -- --check)
    print_success "  interactive formatting OK"
else
    print_warning "  interactive example not found, skipping"
fi

# Check tools formatting
if [ -d "tools/benchmark/rp2040" ]; then
    print_step "  Checking benchmark/rp2040 formatting..."
    (cd tools/benchmark/rp2040 && cargo fmt -- --check)
    print_success "  benchmark/rp2040 formatting OK"
else
    print_warning "  benchmark/rp2040 not found, skipping"
fi

if [ -d "tools/benchmark/rp2350" ]; then
    print_step "  Checking benchmark/rp2350 formatting..."
    (cd tools/benchmark/rp2350 && cargo fmt -- --check)
    print_success "  benchmark/rp2350 formatting OK"
else
    print_warning "  benchmark/rp2350 not found, skipping"
fi

if [ -d "tools/binary-analyzer/test-binary" ]; then
    print_step "  Checking binary-analyzer/test-binary formatting..."
    (cd tools/binary-analyzer/test-binary && cargo fmt -- --check)
    print_success "  binary-analyzer/test-binary formatting OK"
else
    print_warning "  binary-analyzer/test-binary not found, skipping"
fi

if [ -d "tools/sizeof-calculator" ]; then
    print_step "  Checking sizeof-calculator formatting..."
    (cd tools/sizeof-calculator && cargo fmt -- --check)
    print_success "  sizeof-calculator formatting OK"
else
    print_warning "  sizeof-calculator not found, skipping"
fi
echo ""

# 2. Clippy (no features)
print_step "Running Clippy (no default features)..."
cargo clippy --lib --no-default-features -- -D warnings
print_success "Clippy passed (no features)"
echo ""

# 3. Clippy (all features)
print_step "Running Clippy (all features)..."
cargo clippy --lib --all-features -- -D warnings
print_success "Clippy passed (all features)"
echo ""

# 4. Test (no features)
print_step "Running tests (no default features)..."
cargo test --no-default-features
print_success "Tests passed (no features)"
echo ""

# 5. Documentation check
print_step "Building documentation (all features)..."
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features
print_success "Documentation built successfully"
echo ""

print_step "Building documentation (no default features)..."
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --no-default-features
print_success "Documentation built successfully"
echo ""

# 6. no_std compatibility check
print_step "Checking no_std compatibility..."

# Check thumbv6m-none-eabi (Cortex-M0/M0+, no FPU)
if rustup target list --installed | grep -q "thumbv6m-none-eabi"; then
    print_step "  Building for thumbv6m-none-eabi (no FPU)..."
    cargo build --target thumbv6m-none-eabi --lib --release --no-default-features
    print_success "  no_std build OK (no features)"

    cargo build --target thumbv6m-none-eabi --lib --release --no-default-features --features "std-math"
    print_success "  no_std build OK (std-math)"

    cargo build --target thumbv6m-none-eabi --lib --release --no-default-features --features "moving-average"
    print_success "  no_std build OK (moving-average)"

    cargo build --target thumbv6m-none-eabi --lib --release --no-default-features --features "grab-mode"
    print_success "  no_std build OK (grab-mode)"
else
    print_warning "  thumbv6m-none-eabi target not installed, skipping"
    echo "    Install with: rustup target add thumbv6m-none-eabi"
fi

# Check thumbv7em-none-eabihf (Cortex-M4F/M7, with FPU)
if rustup target list --installed | grep -q "thumbv7em-none-eabihf"; then
    print_step "  Building for thumbv7em-none-eabihf (with FPU)..."
    cargo build --target thumbv7em-none-eabihf --lib --release --no-default-features
    print_success "  no_std build OK (no features)"

    cargo build --target thumbv7em-none-eabihf --lib --release --no-default-features --features "std-math"
    print_success "  no_std build OK (std-math)"

    cargo build --target thumbv7em-none-eabihf --lib --release --no-default-features --features "moving-average"
    print_success "  no_std build OK (moving-average)"

    cargo build --target thumbv7em-none-eabihf --lib --release --no-default-features --features "grab-mode"
    print_success "  no_std build OK (grab-mode)"
else
    print_warning "  thumbv7em-none-eabihf target not installed, skipping"
    echo "    Install with: rustup target add thumbv7em-none-eabihf"
fi
echo ""

# 7. Build examples
print_step "Building example: filtering..."
if [ -d "examples/filtering" ]; then
    (cd examples/filtering && cargo build --release)
    print_success "filtering example built"
else
    print_warning "filtering example not found, skipping"
fi
echo ""

print_step "Building example: interactive..."
if [ -d "examples/interactive" ]; then
    (cd examples/interactive && cargo build --release)
    print_success "interactive example built"
else
    print_warning "interactive example not found, skipping"
fi
echo ""

# 8. Binary analyzer (skipped in local CI - run manually if needed)
# print_step "Running binary analyzer..."
# if [ -f "tools/binary-analyzer/generate_report.sh" ]; then
#     (cd tools/binary-analyzer && ./generate_report.sh)
#     print_success "Binary analyzer completed"
# else
#     print_warning "Binary analyzer script not found, skipping"
# fi
# echo ""

# 9. Git status check
print_step "Checking git status..."
if [ -n "$(git status --porcelain)" ]; then
    print_warning "There are uncommitted changes:"
    git status --short
else
    print_success "No uncommitted changes"
fi
echo ""

# Summary
echo "========================================="
echo -e "${GREEN}✓ All CI checks passed!${NC}"
echo "========================================="
echo ""
echo "You can now push your changes with confidence."
echo ""
