#!/bin/bash
STATE="$HOME/.claude/sales-coach-last-id"
LAST_ID=$(cat "$STATE" 2>/dev/null || echo "none")
OUTPUT="/tmp/sales-coach-latest.txt"

# Check for new meeting, then run the skill to analyze it
claude -p "Use the Granola MCP tools to list my recent meetings. Check if the most recent meeting has a different ID than '$LAST_ID'. If it's the same, respond with only the word SKIP. If it's different, respond with the line MEETING_ID:<the new id> then run /sales-coach to analyze it." \
  --allowedTools "mcp__claude_ai_Granola__*" > "$OUTPUT" 2>&1

if grep -q "SKIP" "$OUTPUT"; then
  exit 0
fi

NEW_ID=$(grep -oP 'MEETING_ID:\K\S+' "$OUTPUT" 2>/dev/null)
if [ -n "$NEW_ID" ]; then
  echo "$NEW_ID" > "$STATE"
  osascript -e 'display notification "New sales call analyzed — check /tmp/sales-coach-latest.txt" with title "Sales Coach"'
fi
