#!/usr/bin/env bash
# Pre-push hook: run local checks that match CI before allowing push.
# Install: git config core.hooksPath .githooks

set -euo pipefail

run_ci_like_checks() {
  echo ">>> pre-push: running local checks required before push"

  if ! command -v uv >/dev/null; then
    echo "[pre-push] Missing 'uv' in PATH. Install with: curl -LsSf https://astral.sh/uv/install.sh | sh" >&2
    exit 1
  fi

  echo "==> ruff check"
  uv run ruff check .

  echo "==> ruff format --check"
  uv run ruff format --check .

  echo "==> pyright"
  uv run pyright

  echo "==> gitlint (conventional commits)"
  if git rev-parse --verify --quiet origin/main >/dev/null; then
    # Redirect stdin from /dev/null: git pipes the push ref-line
    # ("<local-ref> <local-sha> <remote-ref> <remote-sha>") to the pre-push hook
    # on stdin, and gitlint would otherwise lint that line as a commit message
    # instead of the --commits range — failing every push with a bogus title error.
    uv run gitlint --commits "origin/main..HEAD" </dev/null
  else
    echo "   skipped: origin/main not found locally (run 'git fetch origin main' to enable)"
  fi

  echo "==> pytest (non-exhaustive)"
  uv run pytest -m "not exhaustive" -x -q

  echo "==> pre-push checks passed"
}

run_ci_like_checks
