#!/bin/sh
# 1. Regenerates game data (npm run data) and stages the generated files.
# 2. Bumps the app version (src/version.json) and stages it.
# Installed via `npm install` (scripts/install-hooks.mjs sets core.hooksPath).
# Skip with: git commit --no-verify
#   or skip one step: SKIP_DATA_BUILD=1 / SKIP_VERSION_BUMP=1 git commit ...

# Nothing staged: let git report "nothing to commit" without doing any work
if git diff --cached --quiet; then
  exit 0
fi

if [ -z "$SKIP_DATA_BUILD" ]; then
  echo "pre-commit: regenerating game data (npm run data)..."
  LOG_FILE="$(git rev-parse --git-dir)/data-build.log"
  if ! npm run data > "$LOG_FILE" 2>&1; then
    echo "pre-commit: npm run data failed, commit aborted. Last lines of $LOG_FILE:"
    tail -n 20 "$LOG_FILE"
    exit 1
  fi
  # Stage generated outputs (no-op for files that didn't change)
  git add src/data/lexicon.js scripts/combo-words.txt || exit 1
fi

if [ -z "$SKIP_VERSION_BUMP" ]; then
  node scripts/bump-version.mjs || exit 1
  git add src/version.json || exit 1
fi
