"""
shared/messaging/events.py
Event definitions for microservices communication
"""
from datetime import datetime, timezone
from typing import Dict, Any, Optional
import uuid

class BaseEvent:
    """Base event class - FIXED"""
    def __init__(self, service_name: str, data: Dict[str, Any], event_type: str = "", correlation_id: Optional[str] = None):
        self.service_name = service_name
        self.data = data
        self.event_type = event_type
        self.event_id = str(uuid.uuid4())
        self.timestamp = datetime.now(timezone.utc)
        self.correlation_id = correlation_id
    
    def to_dict(self) -> Dict[str, Any]:
        """Converts the event object to a dictionary for JSON serialization - FIXED"""
        return {
            "service_name": self.service_name,
            "data": self.data,
            "event_type": self.event_type,
            "event_id": self.event_id,
            "timestamp": self.timestamp.isoformat(),  # Convert datetime to ISO string
            "correlation_id": self.correlation_id
        }

# Auth Events - FIXED
class UserRegisteredEvent(BaseEvent):
    def __init__(self, service_name: str, data: Dict[str, Any], correlation_id: Optional[str] = None):
        super().__init__(
            service_name=service_name,
            data=data,
            event_type="user.registered",
            correlation_id=correlation_id
        )

class UserLoginEvent(BaseEvent):
    def __init__(self, service_name: str, data: Dict[str, Any], correlation_id: Optional[str] = None):
        super().__init__(
            service_name=service_name,
            data=data,
            event_type="user.login",
            correlation_id=correlation_id
        )

class UserLogoutEvent(BaseEvent):
    def __init__(self, service_name: str, data: Dict[str, Any], correlation_id: Optional[str] = None):
        super().__init__(
            service_name=service_name,
            data=data,
            event_type="user.logout",
            correlation_id=correlation_id
        )

# Training Events
class TrainingStartedEvent(BaseEvent):
    def __init__(self, service_name: str, data: Dict[str, Any], correlation_id: Optional[str] = None):
        super().__init__(
            service_name=service_name,
            data=data,
            event_type="training.started",
            correlation_id=correlation_id
        )

class TrainingCompletedEvent(BaseEvent):
    def __init__(self, service_name: str, data: Dict[str, Any], correlation_id: Optional[str] = None):
        super().__init__(
            service_name=service_name,
            data=data,
            event_type="training.completed",
            correlation_id=correlation_id
        )

class TrainingFailedEvent(BaseEvent):
    def __init__(self, service_name: str, data: Dict[str, Any], correlation_id: Optional[str] = None):
        super().__init__(
            service_name=service_name,
            data=data,
            event_type="training.failed",
            correlation_id=correlation_id
        )

# Forecasting Events
class ForecastGeneratedEvent(BaseEvent):
    def __init__(self, service_name: str, data: Dict[str, Any], correlation_id: Optional[str] = None):
        super().__init__(
            service_name=service_name,
            data=data,
            event_type="forecast.generated",
            correlation_id=correlation_id
        )

# Data Events
class DataImportedEvent(BaseEvent):
    def __init__(self, service_name: str, data: Dict[str, Any], correlation_id: Optional[str] = None):
        super().__init__(
            service_name=service_name,
            data=data,
            event_type="data.imported",
            correlation_id=correlation_id
        )
