#!/bin/bash

# Wogi Flow - Archive Old Entries
# Keeps request-log small by archiving old entries

set -e

WORKFLOW_DIR=".workflow"
REQUEST_LOG="$WORKFLOW_DIR/state/request-log.md"
ARCHIVE_DIR="$WORKFLOW_DIR/archive"

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'

show_help() {
    echo "Archive old request-log entries"
    echo ""
    echo "Usage: flow archive [options]"
    echo ""
    echo "Options:"
    echo "  --keep N       Keep last N entries (default: 50)"
    echo "  --before DATE  Archive entries before DATE (YYYY-MM-DD)"
    echo "  --dry-run      Show what would be archived"
    echo ""
    echo "Archives old entries to .workflow/archive/request-log-YYYY-MM.md"
}

KEEP=50
BEFORE=""
DRY_RUN=false

while [[ $# -gt 0 ]]; do
    case $1 in
        --keep)
            KEEP="$2"
            shift 2
            ;;
        --before)
            BEFORE="$2"
            shift 2
            ;;
        --dry-run)
            DRY_RUN=true
            shift
            ;;
        help|--help|-h)
            show_help
            exit 0
            ;;
        *)
            shift
            ;;
    esac
done

if [ ! -f "$REQUEST_LOG" ]; then
    echo -e "${YELLOW}No request-log.md found${NC}"
    exit 0
fi

# Count entries
total_entries=$(grep -c "^### R-" "$REQUEST_LOG" 2>/dev/null || echo "0")

if [ "$total_entries" -le "$KEEP" ]; then
    echo -e "${GREEN}✓ Request-log has $total_entries entries (keeping $KEEP)${NC}"
    echo "Nothing to archive."
    exit 0
fi

to_archive=$((total_entries - KEEP))
echo -e "${CYAN}Request-log: $total_entries entries${NC}"
echo -e "${YELLOW}Archiving: $to_archive oldest entries${NC}"
echo ""

if [ "$DRY_RUN" = true ]; then
    echo "[Dry run - no changes made]"
    echo ""
    echo "Would archive entries R-001 through R-$(printf '%03d' $to_archive)"
    exit 0
fi

# Create archive directory
mkdir -p "$ARCHIVE_DIR"

# Get current month for archive file
ARCHIVE_FILE="$ARCHIVE_DIR/request-log-$(date +%Y-%m).md"

# Extract entries to archive (oldest ones)
echo -e "${CYAN}Extracting old entries...${NC}"

# Use Python for reliable parsing
python3 << EOF
import re
from datetime import datetime

with open('$REQUEST_LOG', 'r') as f:
    content = f.read()

# Split into entries
entries = re.split(r'(?=^### R-\d+)', content, flags=re.MULTILINE)
header = entries[0] if not entries[0].startswith('### R-') else ''
entries = [e for e in entries if e.startswith('### R-')]

keep = $KEEP
to_archive = entries[:-keep] if keep < len(entries) else []
to_keep = entries[-keep:] if keep < len(entries) else entries

# Archive old entries
if to_archive:
    with open('$ARCHIVE_FILE', 'a') as f:
        f.write('\n# Archived Entries\n\n')
        for entry in to_archive:
            f.write(entry)
    
    # Rewrite request-log with only recent entries
    with open('$REQUEST_LOG', 'w') as f:
        f.write(header)
        for entry in to_keep:
            f.write(entry)
    
    print(f'Archived {len(to_archive)} entries to $ARCHIVE_FILE')
    print(f'Kept {len(to_keep)} recent entries')
else:
    print('Nothing to archive')
EOF

echo ""
echo -e "${GREEN}✓ Archive complete${NC}"
echo "  Archived to: $ARCHIVE_FILE"
echo "  Remaining entries: $KEEP"
