from __future__ import annotations

import uuid
from dataclasses import dataclass, field
from datetime import datetime, timezone
from enum import Enum
from typing import Any


class EventType(str, Enum):
    HEARTBEAT = "heartbeat"
    LOGIN_STATUS_CHANGED = "login_status_changed"
    ONLINE_STATUS_CHANGED = "online_status_changed"
    SEND_RESULT = "send_result"
    ERROR = "error"


class EventStatus(str, Enum):
    PENDING = "pending"
    RETRYING = "retrying"
    DELIVERED = "delivered"
    DEAD_LETTER = "dead_letter"


@dataclass
class OutboundEvent:
    event_type: EventType
    account_id: str
    tenant_code: str
    data: dict[str, Any]
    event_id: str = field(default_factory=lambda: str(uuid.uuid4()))
    timestamp: datetime = field(
        default_factory=lambda: datetime.now(timezone.utc)
    )
    status: EventStatus = field(default=EventStatus.PENDING)
    attempt_count: int = field(default=0)

    def to_payload(self) -> dict[str, Any]:
        return {
            "event_id": self.event_id,
            "event_type": self.event_type.value,
            "account_id": self.account_id,
            "tenant_code": self.tenant_code,
            "timestamp": self.timestamp.isoformat(),
            "data": self.data,
        }
