#!/bin/sh
#
# Addon: summarize
#
# Archives the active conversation history as a timestamped Markdown summary in
# `.aico/summaries/YYYYMMDDTHHMMSS_PROJECT_SUMMARY.md`, creates/updates a symlink
# `PROJECT_SUMMARY.md` at project root, resets active history to empty via
# `set-history clear`, and adds the symlink to context if missing.
#
# Uses the existing `summarize` prompts for consistency.

set -eu

if [ "${1-}" = "--usage" ]; then
  echo "Archives active history as dated summary (symlink: PROJECT_SUMMARY.md), resets history, adds to context."
  exit 0
fi

PROJECT_ROOT=$(dirname "$AICO_SESSION_FILE")

if [ -f "PROJECT_SUMMARY.md" ]; then
    if aico status --json | jq -e '.context_files[] | select(.=="PROJECT_SUMMARY.md")' >/dev/null 2>&1; then
        :
    else
        echo "⚠️  PROJECT_SUMMARY.md exists but is missing from context."
        echo "Summarizing without it may fragment your project history."
        printf "Add it to context now? [Y/n] "
        read -r REPLY
        if [ "$REPLY" = "Y" ] || [ "$REPLY" = "y" ]; then
            aico add PROJECT_SUMMARY.md
        else
            echo "Proceeding without existing summary..."
        fi
    fi
fi

SUMMARY_DIR="$PROJECT_ROOT/.aico/summaries"

# 1. Check for active history
DUMP_HISTORY=$(aico dump-history)
if [ -z "$DUMP_HISTORY" ]; then
  echo "No active history to summarize. Aborting."
  exit 0
fi

# 2. Generate summary (captures stdout, hides stderr/token info)
SYSTEM_PROMPT='You are an expert technical writer. Your task is to produce a complete replacement for the `PROJECT_SUMMARY.md` file. You must interpret the conversation log provided on stdin and integrate it into the existing `PROJECT_SUMMARY.md` (found in the context files). The goal is to produce a full, valid Markdown document that preserves the structure, historical details, and rationale of the original while accurately reflecting the latest state and decisions. Do not shrink the summary; preserve detail.'

USER_PROMPT='Your goal is to output the full text of the updated `PROJECT_SUMMARY.md`.
Do not output a diff or instructions to update the file, just the new file content.

1. **Recent Developments**: Start with `### Recent Developments`. Summarize the specific progress and decisions from the conversation log on stdin.
2. **Comprehensive Project Summary**: Follow with `### Comprehensive Project Summary`. Create or update this section to act as a full onboarding document.
   - **Content Scope**: Include the rationale for key decisions, evolution of key concepts and approaches, development narrative, current state, and final outcomes.
   - **Preserve & Integrate**: Use the existing `PROJECT_SUMMARY.md` as the base. Preserve all valid historical details and context for full continuity. Weave new info into the narrative structure.

Output the complete markdown document.'

echo "Generating summary..."
echo
echo "$DUMP_HISTORY" | aico ask --no-history --system-prompt "$SYSTEM_PROMPT" "$USER_PROMPT" 2>/dev/null

# 3. Undo the summarization turn + clear active history
aico undo >/dev/null 2>/dev/null
aico set-history clear

# 4. Write timestamped archive + update symlink
mkdir -p "$SUMMARY_DIR"
TIMESTAMP=$(date +%Y%m%dT%H%M%S)
FILE_NAME="${TIMESTAMP}_PROJECT_SUMMARY.md"
SUMMARY_FILE="$SUMMARY_DIR/${FILE_NAME}"
aico last > "$SUMMARY_FILE"
ln -sf "$SUMMARY_FILE" "$PROJECT_ROOT/PROJECT_SUMMARY.md"

# 5. Add symlink to context if missing (check JSON output)
if aico status --json | jq -e '.context_files[] | select(.=="PROJECT_SUMMARY.md")' >/dev/null 2>&1; then
  :
else
  aico add "$PROJECT_ROOT/PROJECT_SUMMARY.md"
fi

echo "Archived summary to $FILE_NAME and reset active history"
