#!/usr/bin/env python3
"""
Weekly Review Generator
Compiles data from Digital Brain into a weekly review document.
"""

import json
import os
from datetime import datetime, timedelta
from pathlib import Path

# Get the digital brain root (parent of agents/)
BRAIN_ROOT = Path(__file__).parent.parent.parent

def load_jsonl(filepath):
    """Load JSONL file, skipping schema lines."""
    items = []
    if not filepath.exists():
        return items
    with open(filepath, 'r') as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            try:
                data = json.loads(line)
                # Skip schema definition lines
                if '_schema' not in data:
                    items.append(data)
            except json.JSONDecodeError:
                continue
    return items

def get_week_range():
    """Get the start and end of the current week."""
    today = datetime.now()
    start = today - timedelta(days=today.weekday())
    end = start + timedelta(days=6)
    return start.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d')

def analyze_content(week_start):
    """Analyze content published this week."""
    posts = load_jsonl(BRAIN_ROOT / 'content' / 'posts.jsonl')
    ideas = load_jsonl(BRAIN_ROOT / 'content' / 'ideas.jsonl')

    week_posts = [p for p in posts if p.get('published', '') >= week_start]
    new_ideas = [i for i in ideas if i.get('created', '') >= week_start]

    return {
        'posts_published': len(week_posts),
        'new_ideas': len(new_ideas),
        'posts': week_posts
    }

def analyze_network(week_start):
    """Analyze network activity this week."""
    interactions = load_jsonl(BRAIN_ROOT / 'network' / 'interactions.jsonl')

    week_interactions = [i for i in interactions if i.get('date', '') >= week_start]

    return {
        'interactions': len(week_interactions),
        'details': week_interactions
    }

def analyze_metrics():
    """Get latest metrics if available."""
    metrics = load_jsonl(BRAIN_ROOT / 'operations' / 'metrics.jsonl')
    if metrics:
        return metrics[-1]  # Most recent
    return {}

def generate_review():
    """Generate the weekly review output."""
    week_start, week_end = get_week_range()

    content = analyze_content(week_start)
    network = analyze_network(week_start)
    metrics = analyze_metrics()

    review = f"""
# Weekly Review: {week_start} to {week_end}
Generated: {datetime.now().isoformat()}

## Summary

### Content
- Posts published: {content['posts_published']}
- New ideas captured: {content['new_ideas']}

### Network
- Interactions logged: {network['interactions']}

### Latest Metrics
"""

    if metrics:
        audience = metrics.get('audience', {})
        for key, value in audience.items():
            review += f"- {key}: {value}\n"
    else:
        review += "- No metrics recorded yet\n"

    review += """
## Action Items

1. [ ] Review content performance
2. [ ] Plan next week's content
3. [ ] Follow up on pending introductions
4. [ ] Update goals progress
5. [ ] Schedule key meetings

## Notes

[Add your reflections here]
"""

    return review

if __name__ == '__main__':
    print(generate_review())
