#!/bin/zsh
# Re-speak THIS session's last reply, interrupting whatever is playing.
# Uses CLAUDE_CODE_SESSION_ID so it can never grab another terminal's transcript.
V="$HOME/.claude/hooks/voice"
[ -d "$V" ] || V="$HOME/.claude/hooks"

if [ -n "$CLAUDE_CODE_SESSION_ID" ]; then
  T=$(ls ~/.claude/projects/*/"$CLAUDE_CODE_SESSION_ID".jsonl 2>/dev/null | head -1)
fi
# Fallback only when the env var is absent (e.g. the ⌥⇧R global hotkey).
[ -z "$T" ] && T=$(ls -t ~/.claude/projects/*/*.jsonl 2>/dev/null | head -1)
[ -z "$T" ] && { echo "no transcript found"; exit 1 }

# Consume the ONE Stop-hook speech this !command is about to trigger,
# so my own reply doesn't stomp the replay you just asked for.
date +%s > "$HOME/.claude/hooks/voice/suppress-next"

/opt/homebrew/bin/python3 "$HOME/.claude/hooks/voice/replay.py" "$T" && echo "🔁 replaying $(basename $T .jsonl | cut -c1-8)"
