#!/usr/bin/env bash
# AuroraView pre-commit hook
# Ensures Python & Rust lint pass before committing.
# Requires: ruff and cargo available on PATH for their respective checks.

set -euo pipefail

# Resolve ruff command (prefer direct, fallback to uvx)
if command -v ruff >/dev/null 2>&1; then
  RUFF_CMD="ruff"
elif command -v uvx >/dev/null 2>&1; then
  RUFF_CMD="uvx ruff"
elif command -v uv >/dev/null 2>&1; then
  RUFF_CMD="uvx ruff"
else
  RUFF_CMD=""
fi

# Detect changed files in index (staged)
changed_files=$(git diff --cached --name-only --diff-filter=ACM || true)

# ---------- Python (ruff) ----------
if echo "$changed_files" | grep -E '^(python/|tests/).*\.py$' >/dev/null 2>&1; then
  py_changed=$(echo "$changed_files" | grep -E '^(python/|tests/).*\.py$' || true)

  if [ -z "$RUFF_CMD" ]; then
    echo "[pre-commit] ruff not found (and uv/uvx not available); skipping Python checks"
  else
    echo "[pre-commit] Running ruff format on staged Python files"
    $RUFF_CMD format $py_changed

    echo "[pre-commit] Running ruff check --fix on staged Python files"
    $RUFF_CMD check --fix --select=E,F,W --ignore=E501 $py_changed

    echo "[pre-commit] Running ruff check (repo-level)"
    $RUFF_CMD check --select=E,F,W --ignore=E501 python/ tests/
  fi
else
  echo "[pre-commit] Skipping ruff (no Python changes staged)"
fi

# ---------- Rust (fmt + clippy) ----------
# Match: src/**/*.rs, crates/**/*.rs (including tests/), benches/**/*.rs, Cargo.toml, build.rs
if echo "$changed_files" | grep -E '(^src/.*\.rs$|^crates/.*\.rs$|^benches/.*\.rs$|^Cargo\.toml$|^build\.rs$)' >/dev/null 2>&1; then
  if command -v cargo >/dev/null 2>&1; then
    echo "[pre-commit] Running cargo fmt --all"
    cargo fmt --all

    # Verify formatting passes (same as CI check)
    echo "[pre-commit] Verifying cargo fmt --check passes"
    if ! cargo fmt --all -- --check; then
      echo "[pre-commit] ERROR: cargo fmt --check failed after formatting. This should not happen."
      echo "[pre-commit] Please run 'cargo fmt --all' manually and try again."
      exit 1
    fi

    echo "[pre-commit] Running cargo clippy --fix --allow-dirty --allow-staged"
    cargo clippy --fix --allow-dirty --allow-staged --workspace --all-targets --all-features -- -D warnings 2>/dev/null || true

    echo "[pre-commit] Verifying clippy passes"
    cargo clippy --workspace --all-targets --all-features -- -D warnings
  else
    echo "[pre-commit] cargo not found; skipping Rust checks"
  fi
else
  echo "[pre-commit] Skipping cargo (no Rust changes staged)"
fi

echo "[pre-commit] OK"
