#!/bin/bash
# Bulk archive all emails from johan inbox
# Run in background: nohup ./scripts/bulk-archive-johan.sh &

LOG="/home/johan/clawd/memory/johan-archive.log"
echo "$(date): Starting bulk archive" > "$LOG"

while true; do
  # Get next batch of UIDs
  uids=$(curl -s "http://localhost:8025/accounts/johan/messages?folder=INBOX&limit=100" | jq -r '.[].uid' 2>/dev/null)
  
  if [ -z "$uids" ]; then
    echo "$(date): No more emails - done!" >> "$LOG"
    break
  fi
  
  count=0
  for uid in $uids; do
    curl -s -X PATCH "http://localhost:8025/accounts/johan/messages/${uid}?folder=INBOX" \
      -H "Content-Type: application/json" -d '{"move_to": "Archive", "seen": true}' > /dev/null
    count=$((count + 1))
  done
  
  remaining=$(curl -s "http://localhost:8025/accounts/johan/messages?folder=INBOX&limit=2000" | jq 'length' 2>/dev/null)
  echo "$(date): Archived $count, remaining: $remaining" >> "$LOG"
done

echo "$(date): Complete!" >> "$LOG"
