#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0
# Copyright 2024-2026 Lukasz Krzemien (biuro@softspark.eu)
# Source: https://github.com/softspark/ai-toolkit
#
# pre-compact.sh — Save critical context before conversation compaction.
#
# Fires on: PreCompact
# Outputs key context markers so they survive the compaction boundary.
# Priority order: rules > instincts > tasks > session context > git state.
# Skipped when TOOLKIT_HOOK_PROFILE=minimal.

# shellcheck source=_profile-check.sh
source "$(dirname "$0")/_profile-check.sh"
# shellcheck source=_session-paths.sh
source "$(dirname "$0")/_session-paths.sh"

# ---------------------------------------------------------------------------
# 1. Mandatory reload reminder (highest priority — always survives)
# ---------------------------------------------------------------------------
echo "IMPORTANT: Context was compacted. Re-read CLAUDE.md files and active tasks before continuing."

# ---------------------------------------------------------------------------
# 2. Active instincts (high priority — behavioral rules)
# ---------------------------------------------------------------------------
INSTINCTS_DIR=".claude/instincts"
if [ -d "$INSTINCTS_DIR" ] && ls "$INSTINCTS_DIR"/*.md >/dev/null 2>&1; then
    echo "=== Active Instincts ==="
    for f in "$INSTINCTS_DIR"/*.md; do
        # Extract pattern name and confidence from first two lines
        name=$(head -1 "$f" | sed 's/^# Pattern: //')
        confidence=$(grep -m1 'Confidence:' "$f" | awk '{print $2}')
        echo "- [${confidence:-?}] $name"
    done
    echo "========================"
fi

# ---------------------------------------------------------------------------
# 3. Current task state (medium priority — what we're doing)
# ---------------------------------------------------------------------------
if [ -f "$SESSION_CONTEXT_FILE" ]; then
    echo "=== Session Context ==="
    cat "$SESSION_CONTEXT_FILE"
    echo "======================="
fi

# ---------------------------------------------------------------------------
# 4. Git working state (low priority — quick orientation)
# ---------------------------------------------------------------------------
if command -v git >/dev/null 2>&1 && git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
    BRANCH=$(git branch --show-current 2>/dev/null)
    DIRTY=$(git status --porcelain 2>/dev/null | wc -l | tr -d ' ')
    LAST_COMMIT=$(git log -1 --oneline 2>/dev/null)
    echo "=== Git State ==="
    echo "Branch: ${BRANCH:-detached}"
    echo "Uncommitted changes: ${DIRTY}"
    echo "Last commit: ${LAST_COMMIT}"
    echo "================="
fi

# ---------------------------------------------------------------------------
# 5. Key decisions file (if user has been noting decisions this session)
# ---------------------------------------------------------------------------
if [ -f "$SESSION_DECISIONS_FILE" ]; then
    echo "=== Key Decisions This Session ==="
    # Only show last 10 lines to keep token budget tight
    tail -10 "$SESSION_DECISIONS_FILE"
    echo "=================================="
fi

exit 0
