#!/usr/bin/env bash
# pre-commit hook — blocks commits where code files are staged but no documentation is staged.
#
# Doc directories: .docs/  platform/plugins/docs/  .tasks/
# Install: ln -sf ../../platform/scripts/hooks/pre-commit .git/hooks/pre-commit

set -euo pipefail

STAGED=$(git diff --cached --name-only --diff-filter=ACMR 2>/dev/null || true)

if [ -z "$STAGED" ]; then
  exit 0
fi

# ---------------------------------------------------------------------------
# Classify staged files
# ---------------------------------------------------------------------------
CODE_STAGED=false
DOCS_STAGED=false
CODE_FILES=()

# A file is "code" if it has a recognised source extension and lives in a
# source directory.  Everything else (package.json, lock files, brand assets,
# config, .gitignore, etc.) is ignored — only genuine code changes require
# accompanying documentation.
is_code_file() {
  local f="$1"

  # Must be in a source directory
  case "$f" in
    platform/ui/app/*|platform/plugins/*/mcp/src/*|platform/plugins/*/hooks/*|platform/scripts/*|site/src/*) ;;
    *) return 1 ;;
  esac

  # Must have a source extension
  case "$f" in
    *.ts|*.tsx|*.js|*.jsx|*.mjs|*.cjs|*.sh|*.md|*.css) return 0 ;;
    *) return 1 ;;
  esac
}

while IFS= read -r file; do
  if [[ "$file" == .docs/* || "$file" == platform/plugins/docs/* || "$file" == .tasks/* ]]; then
    DOCS_STAGED=true
  elif is_code_file "$file"; then
    CODE_STAGED=true
    CODE_FILES+=("$file")
  fi
  # Files that are neither docs nor code (package.json, lock files, assets) are ignored
done <<< "$STAGED"

if [ "$CODE_STAGED" = false ] || [ "$DOCS_STAGED" = true ]; then
  exit 0
fi

# ---------------------------------------------------------------------------
# Build specific hints from staged code paths (bash 3.2 compatible)
# ---------------------------------------------------------------------------
HINT_HOOKS=false
HINT_MCP=false
HINT_PLUGINS=false
HINT_DEPLOYMENT=false
HINT_WEBCHAT=false
HINT_PLATFORM=false

for file in "${CODE_FILES[@]}"; do
  case "$file" in
    platform/plugins/admin/hooks/*)   HINT_HOOKS=true ;;
    platform/plugins/*/mcp/*)         HINT_MCP=true ;;
    platform/plugins/*/PLUGIN.md|platform/plugins/*) HINT_PLUGINS=true ;;
    platform/scripts/start.sh|platform/scripts/setup.sh) HINT_DEPLOYMENT=true ;;
    site/*)                           HINT_WEBCHAT=true ;;
    platform/*)                       HINT_PLATFORM=true ;;
  esac
done

ANY_HINT=false
for flag in $HINT_HOOKS $HINT_MCP $HINT_PLUGINS $HINT_DEPLOYMENT $HINT_WEBCHAT $HINT_PLATFORM; do
  [ "$flag" = true ] && ANY_HINT=true && break
done

# ---------------------------------------------------------------------------
# Block the commit
# ---------------------------------------------------------------------------
echo ""
echo "┌──────────────────────────────────────────────────────────────┐"
echo "│  COMMIT BLOCKED: documentation not updated                  │"
echo "└──────────────────────────────────────────────────────────────┘"
echo ""
echo "Code files were staged but neither .docs/ nor platform/plugins/docs/"
echo "contains any staged changes."
echo ""

if [ "$ANY_HINT" = true ]; then
  echo "Based on the files you changed, update:"
  [ "$HINT_HOOKS" = true ]      && echo "  • .docs/hooks.md"
  [ "$HINT_MCP" = true ]        && echo "  • .docs/mcp-servers.md"
  [ "$HINT_PLUGINS" = true ]    && echo "  • .docs/plugins.md"
  [ "$HINT_DEPLOYMENT" = true ] && echo "  • .docs/deployment.md"
  [ "$HINT_WEBCHAT" = true ]    && echo "  • .docs/web-chat.md"
  [ "$HINT_PLATFORM" = true ]   && echo "  • .docs/platform.md"
else
  echo "Update the relevant files in:"
  echo "  • .docs/"
  echo "  • platform/plugins/docs/"
fi

echo ""
echo "Stage the updated doc file(s) and re-commit."
echo ""
exit 1
