#!/bin/bash
# PAPI Frontend Design Guard — WARN ONLY, never blocks.
# Fires on PreToolUse for Edit/Write during an active build. Nudges the main build agent to hand
# visible-UI work to the `frontend-design-engineer` subagent instead of hand-writing components —
# the #1 cause of generic, review-failing UI.
#
# Scope: only fires when a PAPI build is active (.papi/active-task-scope.txt exists, written by
# build_execute start, cleared on completeBuild) AND the target is a UI component file
# (.tsx/.jsx/.vue/.svelte) under app/, src/, components/, or pages/. Stays quiet on ad-hoc edits
# and non-UI files.
#
# A hard block is intentionally NOT used: a shell hook cannot tell whether the editor is the main
# build agent or the design agent itself, so blocking would block the agent's own output. The
# warning is self-documenting for that case — the design agent simply proceeds.

INPUT=$(cat)

# Only nudge during an active PAPI build — keep ad-hoc edits silent.
[ -f ".papi/active-task-scope.txt" ] || exit 0

FILE_PATH=$(echo "$INPUT" | python3 -c "
import sys, json
try:
    d = json.load(sys.stdin)
    print(d.get('toolInput', {}).get('file_path', ''))
except Exception:
    print('')
" 2>/dev/null)

[ -z "$FILE_PATH" ] && exit 0

# Only visible-UI component files.
case "$FILE_PATH" in
  *.tsx|*.jsx|*.vue|*.svelte) ;;
  *) exit 0 ;;
esac

# Only inside a UI source tree.
case "$FILE_PATH" in
  */app/*|*/src/*|*/components/*|*/pages/*) ;;
  *) exit 0 ;;
esac

echo "⚠️  FRONTEND DESIGN GUARD: editing a visible-UI component during an active build."
echo "   Target: $FILE_PATH"
echo "   The visual layer should be built by the \`frontend-design-engineer\` subagent (Task tool),"
echo "   not hand-written in the main build context — it runs design-critique + the anti-slop"
echo "   blocklist and verifies in-browser. Hand-written UI fails review."
echo "   If you ARE the frontend-design-engineer agent, this is expected — proceed."
echo "   Warning only, not a block."

exit 0
