#!/bin/bash
#
# index-archives.sh - Build hybrid search index for archived sessions
#
# Uses qmd to create BM25 + vector index over archived transcripts
# Enables semantic search across all historical sessions
#
# Usage: ./index-archives.sh [--rebuild]
#

set -euo pipefail

SESSIONS_DIR="/home/aria/.openclaw/agents/main/sessions"
ARCHIVE_DIR="$SESSIONS_DIR/archive"
INDEX_DIR="$ARCHIVE_DIR/.search-index"
LOG_FILE="/home/aria/.openclaw/workspace/logs/index-archives.log"
REBUILD=false

if [[ "${1:-}" == "--rebuild" ]]; then
    REBUILD=true
    rm -rf "$INDEX_DIR"
fi

log() {
    echo "[$(date -u '+%Y-%m-%d %H:%M:%S UTC')] $*" | tee -a "$LOG_FILE"
}

mkdir -p "$(dirname "$LOG_FILE")"
mkdir -p "$INDEX_DIR"

log "=== Building Archive Search Index ==="

# Check if qmd is available
if ! command -v qmd &> /dev/null; then
    log "WARNING: qmd not found. Install with: pip install qmd"
    log "Falling back to simple file-based index"
    
    # Create simple JSON index for grep-based search
    INDEX_FILE="$ARCHIVE_DIR/search-index.json"
    echo '{"transcripts":[' > "$INDEX_FILE"
    
    FIRST=true
    for archive in "$ARCHIVE_DIR"/20[0-9][0-9]-[0-9][0-9]/*.gz; do
        [[ -f "$archive" ]] || continue
        
        # Extract session info from filename
        BASENAME=$(basename "$archive" .gz)
        
        if [[ "$FIRST" == true ]]; then
            FIRST=false
        else
            echo "," >> "$INDEX_FILE"
        fi
        
        echo "{\"path\":\"$archive\",\"name\":\"$BASENAME\"}" >> "$INDEX_FILE"
    done
    
    echo ']}' >> "$INDEX_FILE"
    log "Simple index created: $(jq '.transcripts | length' "$INDEX_FILE") files indexed"
    exit 0
fi

# Use qmd for hybrid indexing
log "Using qmd for hybrid search indexing"

# Decompress archives to temp dir for indexing
TEMP_DIR=$(mktemp -d)
trap "rm -rf $TEMP_DIR" EXIT

log "Decompressing archives for indexing..."
for archive in "$ARCHIVE_DIR"/20[0-9][0-9]-[0-9][0-9]/*.gz; do
    [[ -f "$archive" ]] || continue
    gunzip -c "$archive" > "$TEMP_DIR/$(basename "$archive" .gz)"
done

TRANSCRIPT_COUNT=$(ls "$TEMP_DIR"/*.jsonl 2>/dev/null | wc -l)
log "Transcripts to index: $TRANSCRIPT_COUNT"

if [[ $TRANSCRIPT_COUNT -gt 0 ]]; then
    # Build qmd index
    qmd index "$TEMP_DIR" --output "$INDEX_DIR" --format jsonl 2>&1 | tee -a "$LOG_FILE"
    log "Hybrid index built successfully"
else
    log "No transcripts to index"
fi

log "Done."
