from typing import Dict, Any, Callable, List
from dataclasses import dataclass, field

@dataclass
class EventSubscriber:
    """Represents an event subscriber"""
    callback: Callable[[Dict[str, Any]], None]
    filters: Dict[str, Any] = field(default_factory=dict)

class EventBus:
    """Event system for workflow and agent coordination"""
    
    def __init__(self):
        self.subscribers: Dict[str, List[EventSubscriber]] = {}
        
    def subscribe(
        self,
        event_type: str,
        callback: Callable[[Dict[str, Any]], None],
        filters: Dict[str, Any] = None
    ):
        """Subscribe to an event type"""
        if event_type not in self.subscribers:
            self.subscribers[event_type] = []
            
        self.subscribers[event_type].append(
            EventSubscriber(callback, filters or {})
        )
        
    def unsubscribe(self, event_type: str, callback: Callable[[Dict[str, Any]], None]):
        """Unsubscribe from an event type"""
        if event_type in self.subscribers:
            self.subscribers[event_type] = [
                s for s in self.subscribers[event_type]
                if s.callback != callback
            ]
            
    def emit(self, event_type: str, data: Dict[str, Any]):
        """Emit an event to all subscribers"""
        if event_type not in self.subscribers:
            return
            
        for subscriber in self.subscribers[event_type]:
            # Check if event matches filters
            matches_filters = True
            for key, value in subscriber.filters.items():
                if key not in data or data[key] != value:
                    matches_filters = False
                    break
                    
            if matches_filters:
                subscriber.callback(data)
