#!/bin/sh
# Pre-push hook: catch the same drift CI surfaces, before the push lands.
#
# Three checks, all fast, none gated by staged diff (pre-push runs after
# commits are made — there is nothing staged to diff). The earlier version
# of this hook gated the agent-docs check on `git diff --cached` which
# meant it never fired at push time; the v0.16.5 release flow tripped over
# that twice in one session, once for site/lib/release.ts drift and once
# for llms.txt drift. Removing the gate is cheaper than getting it right.

# Run rustfmt --check first. Cheap (no compile), fail-fast, and historically
# the gap that bit us: 2026-05-10's silero_smoothing.rs + silero_vad.rs from
# the VAD refactor pushed three trivial rustfmt drifts (auto-fixed in 9f80407)
# that clippy didn't catch but CI's `Format check` job did. The hook used to
# run only clippy + drift checks; without this line, any fmt drift sails
# through to CI red.
echo "Running cargo fmt --check..."
cargo fmt --all -- --check 2>&1
if [ $? -ne 0 ]; then
    echo ""
    echo "Format drift — run: cargo fmt --all"
    exit 1
fi

echo "Running clippy (--no-default-features -D warnings)..."
cargo clippy --all --no-default-features -- -D warnings 2>&1
if [ $? -ne 0 ]; then
    echo ""
    echo "Clippy failed — fix warnings before pushing."
    exit 1
fi

# site/lib/release.ts is generated from manifest.json + test counts. Drift
# fails CI's `Site Release Link Consistency` job. Cheap to re-check.
if [ -f scripts/sync_site_release_version.mjs ]; then
    echo "Running site release constants check..."
    node scripts/sync_site_release_version.mjs --check 2>&1
    if [ $? -ne 0 ]; then
        echo ""
        echo "site/lib/release.ts is stale. Run: node scripts/sync_site_release_version.mjs"
        exit 1
    fi
fi

# llms.txt + the docs/* JSON outputs are generated from the skill catalog
# and manifest. Drift fails CI's `Agent Docs (llms.txt) Consistency` and
# `Agents Skill Sync` jobs.
if [ -f scripts/generate_llms_txt.mjs ]; then
    echo "Running agent docs consistency check..."
    node scripts/generate_llms_txt.mjs --check 2>&1
    if [ $? -ne 0 ]; then
        echo ""
        echo "Agent docs are stale. Run: node scripts/generate_llms_txt.mjs"
        exit 1
    fi
fi

# --- BEGIN BEADS INTEGRATION v1.0.2 ---
# This section is managed by beads. Do not remove these markers.
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-push "$@"
    _bd_exit=$?
    if [ $_bd_exit -eq 124 ]; then
      echo >&2 "beads: hook 'pre-push' timed out after ${_bd_timeout}s — continuing without beads"
      _bd_exit=0
    fi
  else
    bd hooks run pre-push "$@"
    _bd_exit=$?
  fi
  if [ $_bd_exit -eq 3 ]; then
    echo >&2 "beads: database not initialized — skipping hook 'pre-push'"
    _bd_exit=0
  fi
  if [ $_bd_exit -ne 0 ]; then exit $_bd_exit; fi
fi
# --- END BEADS INTEGRATION v1.0.2 ---
