from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING, cast, override

from ai.backend.manager.actions.action import BaseActionResult
from ai.backend.manager.actions.types import ActionOperationType
from ai.backend.manager.data.notification import NotificationChannelData
from ai.backend.manager.repositories.base import Creator
from ai.backend.manager.repositories.notification.creators import NotificationChannelCreatorSpec

from .base import NotificationAction

if TYPE_CHECKING:
    from ai.backend.manager.models.notification import NotificationChannelRow


@dataclass
class CreateChannelAction(NotificationAction):
    """Action to create a notification channel."""

    creator: Creator[NotificationChannelRow]

    @override
    @classmethod
    def operation_type(cls) -> ActionOperationType:
        return ActionOperationType.CREATE

    @override
    def entity_id(self) -> str | None:
        spec = cast(NotificationChannelCreatorSpec, self.creator.spec)
        return spec.name


@dataclass
class CreateChannelActionResult(BaseActionResult):
    """Result of creating a notification channel."""

    channel_data: NotificationChannelData

    @override
    def entity_id(self) -> str | None:
        return str(self.channel_data.id)
