#!/bin/bash
# Pre-push hook for full compliance checks

echo "Running pre-push compliance checks..."

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Check if this hook is properly installed
if [ ! -L ".git/hooks/pre-push" ] && [ ! -f ".git/hooks/pre-push" ]; then
    printf "%b⚠️  WARNING: Git hooks may not be properly installed!%b\n" "${YELLOW}" "${NC}"
    echo "   This check is running but hooks might not trigger automatically."
    echo "   Run '.githooks/install-hooks.sh' to ensure proper installation."
    echo ""
fi

# Track if any check fails
FAILED=0

# Function to run a check
run_check() {
    local name="$1"
    local command="$2"
    
    printf "Checking %s... " "$name"
    if eval "$command" > /dev/null 2>&1; then
        printf "%b✓%b\n" "${GREEN}" "${NC}"
    else
        printf "%b✗%b\n" "${RED}" "${NC}"
        echo "  Run '$command' to see details"
        FAILED=1
    fi
}

echo ""
echo "=== RUST BEST PRACTICES COMPLIANCE CHECK ==="
echo ""

# Core checks (fast)
run_check "code formatting" "cargo fmt -- --check"
run_check "clippy lints" "cargo clippy -- -D warnings"
run_check "tests" "cargo test"
run_check "documentation" "cargo doc --no-deps --document-private-items"

# Security and dependency checks (slower)
echo ""
echo "Running security and dependency checks..."

# Check if cargo-audit is installed
if command -v cargo-audit >/dev/null 2>&1; then
    run_check "security vulnerabilities" "cargo audit"
else
    printf "%b✗ cargo-audit is required for security checks. Run '.githooks/install-tools.sh'%b\n" "${RED}" "${NC}"
    FAILED=1
fi

# Check if cargo-machete is installed
if command -v cargo-machete >/dev/null 2>&1; then
    run_check "unused dependencies" "cargo machete"
else
    printf "%bWarning: cargo-machete not installed. Run '.githooks/install-tools.sh'%b\n" "${YELLOW}" "${NC}"
fi

# Check if cargo-outdated is installed
if command -v cargo-outdated >/dev/null 2>&1; then
    echo ""
    echo "Checking for outdated dependencies..."
    OUTDATED_COUNT=$(cargo outdated --depth 1 2>/dev/null | grep -E "^[a-zA-Z]" | tail -n +3 | wc -l | tr -d ' ')
    if [ "$OUTDATED_COUNT" != "0" ] && [ "$OUTDATED_COUNT" != "" ]; then
        printf "%bNote: %s direct dependencies are outdated%b\n" "${YELLOW}" "$OUTDATED_COUNT" "${NC}"
        echo "  Run 'cargo outdated' to see details"
    else
        printf "%bAll direct dependencies up to date%b\n" "${GREEN}" "${NC}"
    fi
else
    printf "%bWarning: cargo-outdated not installed. Run '.githooks/install-tools.sh'%b\n" "${YELLOW}" "${NC}"
fi

# MSRV check
echo ""
echo "Checking MSRV compatibility..."
MSRV=$(grep "rust-version" Cargo.toml | cut -d'"' -f2)
if [ -n "$MSRV" ]; then
    echo "MSRV is set to: $MSRV"
else
    printf "%bWarning: No MSRV set in Cargo.toml%b\n" "${YELLOW}" "${NC}"
fi

# Check for TODOs in code
echo ""
TODO_COUNT=$(grep -r "TODO\|FIXME\|HACK\|XXX" --include="*.rs" src/ 2>/dev/null | wc -l | tr -d ' ')
if [ "$TODO_COUNT" -gt "0" ]; then
    printf "%bNote: Found %s TODO/FIXME/HACK comments in code%b\n" "${YELLOW}" "$TODO_COUNT" "${NC}"
    echo "  Run 'grep -r \"TODO\\|FIXME\\|HACK\\|XXX\" --include=\"*.rs\" src/' to see them"
fi

# Final result
echo ""
if [ $FAILED -eq 0 ]; then
    printf "%bAll pre-push checks passed!%b\n" "${GREEN}" "${NC}"
    echo "Safe to push to remote repository."
    exit 0
else
    printf "%bPre-push checks failed!%b\n" "${RED}" "${NC}"
    echo "Please fix the issues before pushing."
    echo ""
    echo "To bypass this check (NOT recommended for main branch):"
    echo "  git push --no-verify"
    exit 1
fi
