#!/bin/sh
#
# Pre-push hook: Comprehensive checks before pushing
# Runs: full test suite, clippy strict, doc tests, release build check
#

set -e

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

echo "${CYAN}╭─────────────────────────────────────────╮${NC}"
echo "${CYAN}│           Pre-push Checks               │${NC}"
echo "${CYAN}╰─────────────────────────────────────────╯${NC}"

# 1. Run all tests
echo ""
echo "${YELLOW}▸ Running tests...${NC}"
if ! cargo test --all-features; then
    echo ""
    echo "${RED}✖ Tests failed!${NC}"
    exit 1
fi
echo "${GREEN}✔ All tests passed${NC}"

# 2. Run clippy with strict settings
echo ""
echo "${YELLOW}▸ Running strict clippy checks...${NC}"
if ! cargo clippy --all-targets --all-features -- \
    -D warnings \
    -D clippy::all \
    -D clippy::pedantic \
    -A clippy::module_name_repetitions \
    -A clippy::must_use_candidate \
    -A clippy::missing_errors_doc \
    -A clippy::missing_panics_doc; then
    echo ""
    echo "${RED}✖ Clippy found issues!${NC}"
    exit 1
fi
echo "${GREEN}✔ Clippy strict OK${NC}"

# 3. Check documentation builds
echo ""
echo "${YELLOW}▸ Checking documentation...${NC}"
if ! RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features; then
    echo ""
    echo "${RED}✖ Documentation has warnings or errors!${NC}"
    exit 1
fi
echo "${GREEN}✔ Documentation OK${NC}"

# 4. Verify release build compiles
echo ""
echo "${YELLOW}▸ Verifying release build...${NC}"
if ! cargo build --release --all-features; then
    echo ""
    echo "${RED}✖ Release build failed!${NC}"
    exit 1
fi
echo "${GREEN}✔ Release build OK${NC}"

# 5. Check for any uncommitted changes (safety check)
echo ""
echo "${YELLOW}▸ Checking for uncommitted changes...${NC}"
if ! git diff --quiet; then
    echo "${YELLOW}⚠ Warning: You have uncommitted changes${NC}"
fi
echo "${GREEN}✔ Working directory check complete${NC}"

echo ""
echo "${GREEN}╭─────────────────────────────────────────╮${NC}"
echo "${GREEN}│        ✔ All pre-push checks passed     │${NC}"
echo "${GREEN}╰─────────────────────────────────────────╯${NC}"
echo ""

