#!/bin/bash
# yu-memory passive capture hook
# Invoked by the Claude Code SessionEnd event.
# Lightweight: no LLM, no MCP, no network — appends one JSON line and exits.
set -euo pipefail

CONFIG="$HOME/.yu/config.json"
QUEUE="$HOME/.yu/memory-queue.jsonl"

# Read passiveCapture flag — requires python3 (macOS has it; Linux usually does too)
ENABLED=$(python3 - <<'PYEOF' 2>/dev/null || echo "false"
import json, sys
try:
    with open("$CONFIG") as f:
        data = json.load(f)
    print("true" if data.get("passiveCapture") else "false")
except Exception:
    print("false")
PYEOF
)

if [ "$ENABLED" != "true" ]; then
  exit 0
fi

# Resolve transcript path — Claude Code sets CLAUDE_CODE_TRANSCRIPT_PATH
TRANSCRIPT="${CLAUDE_CODE_TRANSCRIPT_PATH:-}"
SESSION_ID="${CLAUDE_CODE_SESSION_ID:-unknown}"
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")

if [ -z "$TRANSCRIPT" ]; then
  exit 0
fi

mkdir -p "$(dirname "$QUEUE")"

# Append one JSONL entry — double-quote escaping handled by printf
printf '{"transcript_path":"%s","session_id":"%s","timestamp":"%s","processed":false}\n' \
  "$TRANSCRIPT" "$SESSION_ID" "$TIMESTAMP" >> "$QUEUE"

exit 0
