#!/usr/bin/env bash
# Mugi pre-commit hook: gate every commit on the smoke test set when Python files
# change; nag (don't block) when Swift / pbxproj files change so the user runs
# Cmd+U in Xcode before pushing.
#
# Design notes:
#   - Smart by file type: docs-only commits skip smoke entirely.
#   - Chains an existing pre-commit.local hook (if you also use pre-commit framework
#     or husky), so we coexist without silently overwriting.
#   - Bypass with `git commit --no-verify` when you genuinely must; a marker file
#     records the bypass for the post-commit nag.
#   - Reversible via scripts/uninstall_git_hooks.sh.
set -e

repo_root=$(git rev-parse --show-toplevel 2>/dev/null || pwd)
cd "$repo_root"

# Chain a previously-installed hook so we coexist with pre-commit framework, husky, etc.
if [ -x ".git/hooks/pre-commit.local" ]; then
  echo "[mugi-pre-commit] running chained pre-commit.local..."
  ".git/hooks/pre-commit.local" "$@" || exit $?
fi

changed=$(git diff --cached --name-only --diff-filter=ACM)

# Filter out vendored / generated content that shouldn't trigger smoke.
relevant_py=$(echo "$changed" | grep -E '\.py$' | grep -vE '^(\.tmp_|legacy/)' || true)
relevant_swift=$(echo "$changed" | grep -E '\.(swift|pbxproj)$' || true)
# Bundle/requirements changes can break a clean-machine install without touching
# any .py file (e.g. editing copy_backend_bundle.sh DIRS). Smoke includes the
# Layer 0 bundle-completeness guard, so run it when these change too.
relevant_bundle=$(echo "$changed" | grep -E '(^|/)(copy_backend_bundle\.sh|requirements[^/]*\.txt)$' || true)

if [ -n "$relevant_py" ] || [ -n "$relevant_bundle" ]; then
  if [ -n "$relevant_py" ]; then
    echo "[mugi-pre-commit] Python files changed -> running smoke..."
  else
    echo "[mugi-pre-commit] bundle/requirements changed -> running smoke (bundle guard)..."
  fi
  if ! make -s smoke; then
    echo ""
    echo "[mugi-pre-commit] SMOKE FAILED. Commit blocked."
    echo "                  Fix the failing test(s) above, or use 'git commit --no-verify'"
    echo "                  for genuine emergencies (you owe 'make smoke' before pushing)."
    exit 1
  fi
  echo "[mugi-pre-commit] smoke passed."
fi

if [ -n "$relevant_py" ]; then
  echo "[mugi-pre-commit] ALL_CAPS load lint (refactor footgun)..."
  if ! python3 scripts/check_screaming_snake_loads.py; then
    echo ""
    echo "[mugi-pre-commit] CHECK_SCREAMING_SNAKE_LOADS FAILED. Commit blocked."
    echo "                  Fix undefined ALL_CAPS names (usually a missing import after a split)."
    exit 1
  fi
  echo "[mugi-pre-commit] screaming-snake load lint passed."
fi

if [ -n "$relevant_swift" ]; then
  echo ""
  echo "[mugi-pre-commit] Swift / pbxproj files changed -> running SwiftUI modifier lint..."
  if ! python3 scripts/check_swiftui_modifiers.py --diff; then
    echo ""
    echo "[mugi-pre-commit] SwiftUI MODIFIER LINT FAILED. Commit blocked."
    echo "                  See above for the offending patterns."
    exit 1
  fi
  echo "[mugi-pre-commit] SwiftUI modifier lint passed."
  echo "[mugi-pre-commit] Run Cmd+U on the Mugi scheme in Xcode before pushing."
  echo "                  (Or install the optional pre-push hook with"
  echo "                   'bash scripts/install_git_hooks.sh --with-swift'.)"
fi

# Clear any prior bypass marker on a clean run.
rm -f .git/MUGI_SMOKE_BYPASSED 2>/dev/null || true
exit 0
