#!/bin/bash
# =============================================================================
# PRE-COMMIT HOOK — docs up-to-date check
# =============================================================================
# If src/pinky_core/ files are staged:
#   1. Regenerate pdoc and stage the output automatically.
#   2. Require README.md to also be staged.
# =============================================================================

SRC_STAGED=$(git diff --cached --name-only -- src/pinky_core/ | grep -v '__pycache__' | wc -l | tr -d ' ')

if [ "$SRC_STAGED" -eq 0 ]; then
    exit 0
fi

# 1. Regenerate pdoc
if command -v pdoc > /dev/null 2>&1; then
    pdoc pinky_core -o docs/reference/generated/api/pinky-core/ --no-show-source --template-dir docs/templates/ 2>/dev/null
    git add docs/reference/generated/api/pinky-core/ 2>/dev/null
else
    echo "⚠️  pdoc not found — skipping API doc regeneration."
    echo "   Install with: pip install pdoc"
fi

# 2. Require README.md to be staged
README_STAGED=$(git diff --cached --name-only -- README.md | wc -l | tr -d ' ')

if [ "$README_STAGED" -eq 0 ]; then
    echo "❌ README.md not staged."
    echo ""
    echo "   src/pinky_core/ was modified — README.md must be updated"
    echo "   and staged before committing."
    exit 1
fi

exit 0
