#!/usr/bin/env bash
# Description: Comprehensive Clippy audit with all lints and pedantic checks

set -e

# Unified help handler
SCRIPT_NAME="$(basename "$0")"
DESC="$(grep -m1 '^# Description:' "$0" | sed 's/^# Description:[[:space:]]*//')"
print_help() { echo "Usage: $SCRIPT_NAME"; [ -n "$DESC" ] && echo "$DESC"; exit 0; }
case "${1:-}" in -h|--help|help) print_help ;; esac

# Resolve repo root
find_repo_root() {
  local d
  d="$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd)"
  while [ "$d" != "/" ]; do
    if [ -f "$d/Cargo.toml" ]; then echo "$d"; return; fi
    d="$(dirname "$d")"
  done
  echo "."
}
ROOT="$(find_repo_root)"; cd "$ROOT" || exit 1

echo "Running comprehensive Clippy audit..."

# Allow feature override to avoid platform-incompatible features (e.g., xdp on macOS)
# Example: QUICFUSCATE_CARGO_FEATURES="--features pq,with_aegis" ./scripts/Audits/audit-clippy-comprehensive.sh
FEAT_FLAG="${QUICFUSCATE_CARGO_FEATURES:---all-features}"

# Standard clippy check
echo "1. Standard Clippy check..."
cargo clippy --all-targets ${FEAT_FLAG} -- -D warnings

# Pedantic clippy check
echo "2. Pedantic Clippy check..."
cargo clippy --all-targets ${FEAT_FLAG} -- -W clippy::pedantic

# Nursery clippy check (experimental lints)
echo "3. Nursery Clippy check..."
cargo clippy --all-targets ${FEAT_FLAG} -- -W clippy::nursery

# Cargo clippy check (cargo-specific lints)
echo "4. Cargo-specific Clippy check..."
cargo clippy --all-targets ${FEAT_FLAG} -- -W clippy::cargo

# Performance-focused clippy check
echo "5. Performance Clippy check..."
cargo clippy --all-targets ${FEAT_FLAG} -- -W clippy::perf

# Complexity clippy check
echo "6. Complexity Clippy check..."
cargo clippy --all-targets ${FEAT_FLAG} -- -W clippy::complexity

# Style clippy check
echo "7. Style Clippy check..."
cargo clippy --all-targets ${FEAT_FLAG} -- -W clippy::style

# Correctness clippy check
echo "8. Correctness Clippy check..."
cargo clippy --all-targets ${FEAT_FLAG} -- -W clippy::correctness

# Suspicious clippy check
echo "9. Suspicious Clippy check..."
cargo clippy --all-targets ${FEAT_FLAG} -- -W clippy::suspicious

# Release mode clippy check
echo "10. Release mode Clippy check..."
cargo clippy --release --all-targets ${FEAT_FLAG} -- -D warnings

echo "✓ Comprehensive Clippy audit completed successfully!"