#!/bin/bash
# Sync agent souls from Notion to local SOUL.md files
# Run via cron every 15 minutes
# Updated 2026-02-06: Now syncs ALL 8 agents

NOTION_KEY=$(cat ~/.config/notion/api_key)

# Agent Notion page IDs (all 8 agents)
KITT_PAGE="2ff330c2-8646-812a-bc89-d3afefa7f349"
ANTON_PAGE="2fb330c2-8646-814b-93a4-d07e2a727fb6"
OGILVY_PAGE="2fb330c2-8646-81cc-83cb-f240de0170a8"
TATIANA_PAGE="2fb330c2-8646-8112-8c03-d93bc21c454a"
ERICA_PAGE="2ff330c2-8646-814b-bf3d-d20b3f346e99"
JULIA_PAGE="2ff330c2-8646-81cc-9191-c0f92fe99e05"
JESSICA_PAGE="2ff330c2-8646-8199-a0f0-c4e22f8d8aa7"
THIBAULT_PAGE="2ff330c2-8646-813e-8041-f2e98de00081"

# Target directories
KITT_DIR="$HOME/.openclaw/agents/kitt"
ANTON_DIR="$HOME/.clawdbot/agents/anton/agent"
OGILVY_DIR="$HOME/.clawdbot/agents/ogilvy/agent"
TATIANA_DIR="$HOME/.clawdbot/agents/tatiana/agent"
ERICA_DIR="$HOME/.openclaw/agents/erica"
JULIA_DIR="$HOME/.openclaw/agents/julia"
JESSICA_DIR="$HOME/.openclaw/agents/jessica"
THIBAULT_DIR="$HOME/.openclaw/agents/thibault"

sync_agent() {
    local page_id=$1
    local agent_dir=$2
    local agent_name=$3
    
    # Ensure directory exists
    mkdir -p "$agent_dir"
    
    echo "Syncing $agent_name..."
    
    # Get page content from Notion
    content=$(curl -s "https://api.notion.com/v1/blocks/$page_id/children?page_size=100" \
        -H "Authorization: Bearer $NOTION_KEY" \
        -H "Notion-Version: 2022-06-28" | python3 -c "
import sys, json
data = json.load(sys.stdin)
output = []
for block in data.get('results', []):
    btype = block.get('type')
    if btype == 'heading_1':
        texts = block.get('heading_1', {}).get('rich_text', [])
        text = ''.join([t.get('plain_text', '') for t in texts])
        output.append(f'# {text}')
    elif btype == 'heading_2':
        texts = block.get('heading_2', {}).get('rich_text', [])
        text = ''.join([t.get('plain_text', '') for t in texts])
        output.append(f'## {text}')
    elif btype == 'heading_3':
        texts = block.get('heading_3', {}).get('rich_text', [])
        text = ''.join([t.get('plain_text', '') for t in texts])
        output.append(f'### {text}')
    elif btype == 'paragraph':
        texts = block.get('paragraph', {}).get('rich_text', [])
        text = ''.join([t.get('plain_text', '') for t in texts])
        if text:
            output.append(text)
    elif btype == 'bulleted_list_item':
        texts = block.get('bulleted_list_item', {}).get('rich_text', [])
        text = ''.join([t.get('plain_text', '') for t in texts])
        output.append(f'- {text}')
    elif btype == 'numbered_list_item':
        texts = block.get('numbered_list_item', {}).get('rich_text', [])
        text = ''.join([t.get('plain_text', '') for t in texts])
        output.append(f'1. {text}')
    elif btype == 'divider':
        output.append('---')
print('\n'.join(output))
")
    
    # Only update if we got content
    if [ -n "$content" ]; then
        echo "# SOUL.md — $agent_name" > "$agent_dir/SOUL.md"
        echo "# Synced from Notion: $(date)" >> "$agent_dir/SOUL.md"
        echo "" >> "$agent_dir/SOUL.md"
        echo "$content" >> "$agent_dir/SOUL.md"
        echo "✅ $agent_name synced"
    else
        echo "⚠️ No content for $agent_name"
    fi
}

# Sync all 8 agents
sync_agent "$KITT_PAGE" "$KITT_DIR" "Kitt"
sync_agent "$ANTON_PAGE" "$ANTON_DIR" "Anton Ego"
sync_agent "$OGILVY_PAGE" "$OGILVY_DIR" "David Ogilvy"
sync_agent "$TATIANA_PAGE" "$TATIANA_DIR" "Tatiana"
sync_agent "$ERICA_PAGE" "$ERICA_DIR" "Erica Remmele"
sync_agent "$JULIA_PAGE" "$JULIA_DIR" "Julia Louis-Dreyfus"
sync_agent "$JESSICA_PAGE" "$JESSICA_DIR" "Jessica Walsh"
sync_agent "$THIBAULT_PAGE" "$THIBAULT_DIR" "Thibault"

echo "Done: $(date)"
