"""
INTER-AGENT COMMUNICATION
Agents share findings, request help, collaborate
"""
import json
from datetime import datetime
from collections import deque

class SharedMessageBus:
    """Communication channel between agents"""
    
    def __init__(self):
        self.messages = deque(maxlen=100)
        self.agent_inboxes = {}
    
    def broadcast(self, sender, message_type, content):
        """Broadcast to all agents"""
        msg = {
            'timestamp': datetime.now().isoformat(),
            'sender': sender,
            'type': message_type,
            'content': content,
            'id': len(self.messages)
        }
        self.messages.append(msg)
        
        for agent_name in self.agent_inboxes:
            if agent_name != sender:
                self.agent_inboxes[agent_name].append(msg)
        
        return msg['id']
    
    def register_agent(self, agent_name):
        """Register agent"""
        if agent_name not in self.agent_inboxes:
            self.agent_inboxes[agent_name] = []
    
    def get_messages(self, agent_name):
        """Get unread messages"""
        return self.agent_inboxes.get(agent_name, [])
    
    def clear_inbox(self, agent_name):
        """Mark as read"""
        if agent_name in self.agent_inboxes:
            count = len(self.agent_inboxes[agent_name])
            self.agent_inboxes[agent_name] = []
            return count
        return 0

message_bus = SharedMessageBus()
