#!/usr/bin/env bash
# Lorekeeper pre-commit hook
# 1. Blocks commits on main (branch guard)
# 2. Runs lint and tests before every commit. Blocks on failure.
#
# Install: scripts/setup.sh  (run once per clone)

set -euo pipefail

REPO_ROOT=$(git rev-parse --show-toplevel)
cd "$REPO_ROOT"

echo "🔒  Checking branch..."
bash scripts/check-branch.sh
echo "✅  branch ok"

echo ""
echo "🎫  Validating ticket format..."
bash scripts/validate-ticket.sh
echo "✅  tickets valid"

echo ""
echo "🔍  ruff check..."
uv run ruff check src tests scripts/
echo "✅  ruff passed"

echo ""
echo "🔍  mypy..."
uv run mypy src
echo "✅  mypy passed"

echo ""
echo "🔍  biome check (JS)..."
npx --yes @biomejs/biome check src/lorekeeper/dashboard/static/js/
echo "✅  biome passed"

echo ""
echo "🔍  prettier check (Markdown)..."
while IFS= read -r -d '' f; do
  [ -f "$f" ] && printf '%s\0' "$f"
done < <(git ls-files -z '*.md') | xargs -0 npx --yes prettier@3.5.3 --check --prose-wrap preserve
echo "✅  prettier passed"

echo ""
echo "🔍  MCP tool documentation check..."
uv run python scripts/check_mcp_docs.py
echo "✅  mcp docs ok"

echo ""
echo "🔍  Skill format check..."
uv run python scripts/check_skills.py
echo "✅  skills ok"

echo ""
echo "🧪  pytest..."
uv run pytest tests/ -q -x --tb=short
echo "✅  tests passed"

echo ""
echo "✅  All checks passed — proceeding with commit"
