#!/usr/bin/env bash
# Brentwood project — SessionStart recap hook.
# If the project hasn't been worked on for >=12h, tell Claude to catch the user
# up from HANDOFF.md. Shared across all brentwood-dev team members; per-project
# (any team member's activity resets the timer via a shared marker file).
set -u

MARKER=/srv/apps/brentwood/.claude/.last-active
HANDOFF=/srv/apps/brentwood/HANDOFF.md
THRESHOLD=43200   # 12 hours, in seconds

now=$(date +%s)
last=0
[ -f "$MARKER" ] && last=$(stat -c %Y "$MARKER" 2>/dev/null || echo 0)
gap=$(( now - last ))

# Record this session's activity (group-writable so any brentwood-dev member can update it).
touch "$MARKER" 2>/dev/null || true
chmod 0664 "$MARKER" 2>/dev/null || true

# Fire the recap if we've never recorded activity, or it's been >=12h since last work.
if [ "$last" -eq 0 ] || [ "$gap" -ge "$THRESHOLD" ]; then
  if [ "$last" -eq 0 ]; then
    since="no recent activity recorded"
  else
    since="~$(( gap / 3600 ))h since it was last worked on"
  fi
  ctx="You're starting work on the Brentwood website-migration project and it hasn't been touched in a while (${since}). Before doing anything else, read ${HANDOFF} and give the user a concise catch-up: current status, the most recent diary entries (what changed last), any open items or decisions waiting on them, and what's next. If this looks like a new team member, walk them through the 'Start here' onboarding flow and their first task in ${HANDOFF}. Do NOT surface financial figures (pricing / payment / retainer)."
  jq -n --arg c "$ctx" '{hookSpecificOutput:{hookEventName:"SessionStart",additionalContext:$c}}'
fi
exit 0
