#!/usr/bin/env python3
"""
Content Ideas Generator
Generates content ideas based on knowledge base and past successful content.
"""

import json
import argparse
from datetime import datetime
from pathlib import Path

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)
                if '_schema' not in data:
                    items.append(data)
            except json.JSONDecodeError:
                continue
    return items

def get_top_performing_content():
    """Get posts with highest engagement."""
    posts = load_jsonl(BRAIN_ROOT / 'content' / 'posts.jsonl')

    # Sort by engagement metrics if available
    def engagement_score(post):
        metrics = post.get('metrics', {})
        return (
            metrics.get('likes', 0) +
            metrics.get('comments', 0) * 2 +
            metrics.get('reposts', 0) * 3
        )

    sorted_posts = sorted(posts, key=engagement_score, reverse=True)
    return sorted_posts[:5]

def get_recent_bookmarks(category=None):
    """Get recent bookmarks, optionally filtered by category."""
    bookmarks = load_jsonl(BRAIN_ROOT / 'knowledge' / 'bookmarks.jsonl')

    if category:
        bookmarks = [b for b in bookmarks if b.get('category') == category]

    # Sort by date, most recent first
    bookmarks.sort(key=lambda x: x.get('saved_at', ''), reverse=True)
    return bookmarks[:10]

def get_undeveloped_ideas():
    """Get ideas that haven't been developed yet."""
    ideas = load_jsonl(BRAIN_ROOT / 'content' / 'ideas.jsonl')

    raw_ideas = [i for i in ideas if i.get('status') == 'raw']
    return raw_ideas

def generate_suggestions(pillar=None, count=5):
    """Generate content suggestions."""

    output = f"""
# Content Ideas Generator
Generated: {datetime.now().isoformat()}
Filter: {pillar or 'All pillars'}

## Based on Top Performing Content
"""

    top_posts = get_top_performing_content()
    if top_posts:
        output += "\nYour best performing content themes:\n"
        for post in top_posts[:3]:
            output += f"- {post.get('pillar', 'Unknown')}: {post.get('type', 'post')}\n"
        output += "\n**Suggestion**: Create more content in these high-performing areas.\n"
    else:
        output += "\nNo post history yet. Start creating!\n"

    output += """
## From Your Knowledge Base
"""

    bookmarks = get_recent_bookmarks(pillar)
    if bookmarks:
        output += "\nRecent topics you've been researching:\n"
        for bm in bookmarks[:5]:
            output += f"- {bm.get('title', 'Untitled')} ({bm.get('category', 'uncategorized')})\n"
            if bm.get('key_insights'):
                output += f"  Key insight: {bm['key_insights'][0]}\n"
        output += "\n**Suggestion**: Turn these research topics into educational content.\n"
    else:
        output += "\nNo bookmarks yet. Save interesting content to fuel ideas.\n"

    output += """
## Undeveloped Ideas
"""

    ideas = get_undeveloped_ideas()
    if ideas:
        output += f"\nYou have {len(ideas)} undeveloped ideas:\n"
        for idea in ideas[:count]:
            output += f"- [{idea.get('priority', 'medium')}] {idea.get('idea', 'No content')}\n"
        output += "\n**Suggestion**: Pick one high-priority idea and develop it today.\n"
    else:
        output += "\nNo undeveloped ideas in the queue.\n"

    output += """
## Quick Prompts

1. "What's one thing I learned this week that others would find valuable?"
2. "What's a common mistake I see in my industry?"
3. "What question do I get asked most often?"
4. "What worked for me that's counterintuitive?"
5. "What do I wish I knew when I started?"
"""

    return output

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Generate content ideas')
    parser.add_argument('--pillar', '-p', help='Filter by content pillar')
    parser.add_argument('--count', '-c', type=int, default=5, help='Number of ideas to show')

    args = parser.parse_args()
    print(generate_suggestions(args.pillar, args.count))
