#!/bin/bash
# ================================================================
# session-handoff.sh — Auto-save session state for next session
# ================================================================
# PURPOSE:
#   When a Claude Code session ends (Stop event), automatically
#   captures the current state and writes a handoff file that the
#   next session can read to resume work.
#
#   Solves the "I forgot what I was doing" problem after /compact
#   or session restart.
#
# TRIGGER: Stop
# MATCHER: ""
#
# WHAT IT CAPTURES:
#   - Current git branch and uncommitted changes
#   - Last 5 commands from shell history
#   - Modified files (git diff --stat)
#   - Current working directory
#   - Timestamp
#
# OUTPUT: ~/.claude/session-handoff.md
#   Next session can read this to understand where the last one left off.
#
# CONFIGURATION:
#   CC_HANDOFF_FILE — path to handoff file
#     default: ~/.claude/session-handoff.md
# ================================================================

HANDOFF="${CC_HANDOFF_FILE:-$HOME/.claude/session-handoff.md}"

# Detect project directory from recent tool calls
PROJECT_DIR=""
if [ -d ".git" ]; then
    PROJECT_DIR="$(pwd)"
elif [ -f "$HOME/.claude/projects" ]; then
    PROJECT_DIR=$(ls -td "$HOME/.claude/projects/"*/ 2>/dev/null | head -1)
fi

{
    echo "# Session Handoff"
    echo ""
    echo "**Last session ended:** $(date -Iseconds)"
    echo ""

    # Git state
    if command -v git &>/dev/null && [ -d ".git" ]; then
        BRANCH=$(git branch --show-current 2>/dev/null || echo "unknown")
        echo "## Git State"
        echo "- Branch: \`$BRANCH\`"

        MODIFIED=$(git diff --stat 2>/dev/null | tail -1)
        if [ -n "$MODIFIED" ]; then
            echo "- Changes: $MODIFIED"
            echo ""
            echo "### Modified files"
            echo '```'
            git diff --name-only 2>/dev/null | head -10
            echo '```'
        else
            echo "- Changes: clean working tree"
        fi

        STAGED=$(git diff --cached --stat 2>/dev/null | tail -1)
        if [ -n "$STAGED" ]; then
            echo "- Staged: $STAGED"
        fi

        LAST_COMMIT=$(git log --oneline -1 2>/dev/null)
        echo "- Last commit: $LAST_COMMIT"
        echo ""
    fi

    # Recent blocked commands (if any)
    BLOCK_LOG="$HOME/.claude/blocked-commands.log"
    if [ -f "$BLOCK_LOG" ]; then
        RECENT_BLOCKS=$(tail -3 "$BLOCK_LOG" 2>/dev/null)
        if [ -n "$RECENT_BLOCKS" ]; then
            echo "## Recent Blocks"
            echo '```'
            echo "$RECENT_BLOCKS"
            echo '```'
            echo ""
        fi
    fi

    # Session errors
    ERROR_LOG="$HOME/.claude/session-errors.log"
    if [ -f "$ERROR_LOG" ]; then
        RECENT_ERRORS=$(tail -3 "$ERROR_LOG" 2>/dev/null)
        if [ -n "$RECENT_ERRORS" ]; then
            echo "## Recent Errors"
            echo '```'
            echo "$RECENT_ERRORS"
            echo '```'
            echo ""
        fi
    fi

    echo "---"
    echo "*Auto-generated by session-handoff hook. Read this at session start to resume.*"

} > "$HANDOFF"

exit 0
