#!/bin/sh

# Pre-commit hook: ensures formatting and tests pass before committing

echo "Running pre-commit checks..."

# 1. Check formatting on staged files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(ts|tsx|js|jsx|json|md)$')

if [ -n "$STAGED_FILES" ]; then
  echo "Checking format..."
  echo "$STAGED_FILES" | xargs deno fmt --check
  if [ $? -ne 0 ]; then
    echo ""
    echo "Format check failed. Run 'deno fmt' to fix, then stage the changes."
    exit 1
  fi
fi

# 2. Run all tests
echo "Running tests..."
deno task test
if [ $? -ne 0 ]; then
  echo ""
  echo "Tests failed. Fix the failing tests before committing."
  exit 1
fi

echo "All checks passed."
