#!/usr/bin/env bash
# design-sync-reminder — single hook, both directions (#388). Keeps the Claude Design project
# (`ChicoryTV Design System`, eb3b6122 / local mirror `design-system/`) in step with the shipped
# SPA. Trigger is PURELY MECHANICAL: "touching the UI" == a file matching UI_RE below. No prompt
# keyword guessing. Wired to two boundaries:
#
#   start   (PreToolUse / Write|Edit) — the FIRST time this session edits a UI file, remind to PULL
#                                       the current design from Claude Design first.
#   finish  (Stop)                    — if the working tree actually changed a UI file, remind to
#                                       MIRROR/PUSH the change back before wrapping up.
#
# UI_RE is the one place the "what counts as UI" fileset is defined: SPA .tsx/.css under web/src
# (test files excluded). Widen it here if the design surface grows.
#
# Fail-open: any parse trouble / non-match → emit nothing, exit 0. Throttled once per session per
# phase so it informs without nagging. DesignSync runs only from the main session (docs/design-sync.md).
# This is a reminder, never a hard gate — `start` only injects context; `finish` is a one-shot Stop nudge.
set -euo pipefail

UI_RE='(^|/)web/src/.*\.(tsx|css)$'
TEST_RE='\.test\.(tsx|ts)$'

phase="${1:-}"
input=$(cat)
me=$(printf '%s' "$input" | jq -r '.session_id // "nosess"' 2>/dev/null || true)
cwd=$(printf '%s' "$input" | jq -r '.cwd // ""' 2>/dev/null || true)
[ -z "$cwd" ] && cwd="$PWD"
marker="${TMPDIR:-/tmp}/ctv-designsync-${phase}-${me}"

case "$phase" in
  start)
    fp=$(printf '%s' "$input" | jq -r '.tool_input.file_path // ""' 2>/dev/null || true)
    [ -z "$fp" ] && exit 0
    printf '%s' "$fp" | grep -qE "$TEST_RE" && exit 0     # skip test files
    printf '%s' "$fp" | grep -qE "$UI_RE" || exit 0        # not a UI file → nothing
    [ -f "$marker" ] && exit 0
    : > "$marker" 2>/dev/null || true
    read -r -d '' MSG <<'EOF' || true
[design-sync #388] About to edit a ChicoryTV SPA UI file. The `design-system/` prototypes mirror the Claude Design project (eb3b6122). If you're changing how a screen LOOKS, first PULL its current prototype from Claude Design so you start from the live design (docs/design-sync.md, pull = DesignSync list_files/get_file → design-system/, incremental). You'll be reminded to MIRROR the change back when the task finishes. DesignSync runs only from the main session.
EOF
    jq -n --arg m "$MSG" '{hookSpecificOutput:{hookEventName:"PreToolUse",additionalContext:$m}}'
    exit 0
    ;;
  finish)
    # Did this turn actually change a UI file? (tracked diff vs HEAD + untracked, minus tests)
    changed=$( { git -C "$cwd" diff --name-only HEAD 2>/dev/null; git -C "$cwd" ls-files --others --exclude-standard 2>/dev/null; } | grep -vE "$TEST_RE" | grep -E "$UI_RE" || true )
    [ -z "$changed" ] && exit 0
    [ -f "$marker" ] && exit 0
    : > "$marker" 2>/dev/null || true
    n=$(printf '%s\n' "$changed" | sed '/^$/d' | wc -l | tr -d ' ')
    reason="[design-sync #388] This task changed ${n} SPA UI file(s) under web/src. Before wrapping up, MIRROR the visual change into the matching design-system/templates/chicorytv-admin/*.jsx prototype and push it to Claude Design (eb3b6122) in this same session, per docs/design-sync.md — so the design system does not drift from prod. If you already synced, or are deliberately deferring the mirror (say why), just note it and stop. DesignSync runs only from the main session. This one-shot reminder won't fire again this session."
    jq -n --arg r "$reason" '{decision:"block",reason:$r}'
    exit 0
    ;;
  *)
    exit 0
    ;;
esac
