#!/usr/bin/env sh
# Quality-gate pre-commit hook.
# Runs fast checks on staged files only — the full CI chain runs on push.

# --- BEGIN BEADS INTEGRATION v0.62.0 ---
# Mirrors the beads-managed block from .git/hooks/pre-commit so beads keeps working
# after Husky takes over core.hooksPath. Re-sync if beads upgrades its hook.
if command -v bd >/dev/null 2>&1; then
  export BD_GIT_HOOK=1
  _bd_timeout=${BEADS_HOOK_TIMEOUT:-300}
  if command -v timeout >/dev/null 2>&1; then
    timeout "$_bd_timeout" bd hooks run pre-commit "$@"
    _bd_exit=$?
    if [ $_bd_exit -eq 124 ]; then
      echo >&2 "beads: hook 'pre-commit' timed out after ${_bd_timeout}s — continuing without beads"
      _bd_exit=0
    fi
  else
    bd hooks run pre-commit "$@"
    _bd_exit=$?
  fi
  if [ $_bd_exit -eq 3 ]; then
    echo >&2 "beads: database not initialized — skipping hook 'pre-commit'"
    _bd_exit=0
  fi
  if [ $_bd_exit -ne 0 ]; then exit $_bd_exit; fi
fi
# --- END BEADS INTEGRATION v0.62.0 ---

# --- Quality gates on staged files ---
bunx lint-staged
