#!/usr/bin/env bash
set -euo pipefail

# Pre-commit hook: run version bump checks when WIT or extension sources change.
# Install: git config core.hooksPath .githooks

# Only run the check if relevant files are staged
STAGED=$(git diff --cached --name-only)

NEEDS_CHECK=false
if echo "$STAGED" | grep -qE '^wit/|^channels-src/|^tools-src/'; then
    NEEDS_CHECK=true
fi

if $NEEDS_CHECK; then
    echo "pre-commit: checking version bumps..."
    if ! ./scripts/check-version-bumps.sh; then
        echo ""
        echo "Commit blocked: version bump check failed."
        echo "Bump versions in the relevant registry JSON and/or WIT package declaration."
        echo "To bypass: git commit --no-verify"
        exit 1
    fi
fi

# i18n parity: when any language pack changes, all languages must stay in sync.
if echo "$STAGED" | grep -qE '^crates/ironclaw_gateway/static/i18n/.*\.js$'; then
    echo "pre-commit: checking i18n parity..."
    if ! ./scripts/check-i18n-parity.sh; then
        echo ""
        echo "Commit blocked: i18n parity check failed."
        echo "Every key added to en.js must also be added to all other language files (zh-CN.js, ko.js, ...)."
        echo "Placeholder tokens like {name} must match across all languages."
        echo "To bypass: git commit --no-verify"
        exit 1
    fi
fi
