from __future__ import annotations

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

from ai.backend.common.types import SessionId
from ai.backend.manager.actions.action import BaseActionResult, BaseBatchActionResult
from ai.backend.manager.actions.types import ActionOperationType
from ai.backend.manager.data.session.types import SessionData
from ai.backend.manager.services.session.base import SessionAction, SessionBatchAction

if TYPE_CHECKING:
    from ai.backend.manager.models.user import UserRole


@dataclass
class CheckAndTransitStatusAction(SessionAction):
    user_id: uuid.UUID
    user_role: UserRole
    session_id: SessionId

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

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


@dataclass
class CheckAndTransitStatusActionResult(BaseActionResult):
    # TODO: Add proper type
    result: dict[SessionId, str]
    session_data: SessionData

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


# TODO: Change this to BatchAction
@dataclass
class CheckAndTransitStatusBatchAction(SessionBatchAction):
    user_id: uuid.UUID
    user_role: UserRole
    session_ids: list[SessionId]

    @override
    def entity_ids(self) -> list[str]:
        return [str(session_id) for session_id in self.session_ids]

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


@dataclass
class CheckAndTransitStatusBatchActionResult(BaseBatchActionResult):
    # TODO: Add proper type
    session_status_map: dict[SessionId, str]

    @override
    def entity_ids(self) -> list[str]:
        return [str(session_id) for session_id in self.session_status_map.keys()]
