import uuid
from collections.abc import Mapping
from dataclasses import dataclass
from typing import Any, override

from ai.backend.common.types import AccessKey, SessionTypes
from ai.backend.manager.actions.action import BaseActionResult
from ai.backend.manager.actions.types import ActionOperationType
from ai.backend.manager.models.user import UserRole
from ai.backend.manager.services.session.base import SessionAction


@dataclass
class CreateClusterAction(SessionAction):
    session_name: str
    user_id: uuid.UUID
    user_role: UserRole
    sudo_session_enabled: bool
    template_id: uuid.UUID
    session_type: SessionTypes
    group_name: str
    domain_name: str
    scaling_group_name: str
    requester_access_key: AccessKey
    owner_access_key: AccessKey
    tag: str
    enqueue_only: bool
    keypair_resource_policy: dict[str, Any] | None
    max_wait_seconds: int

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

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


@dataclass
class CreateClusterActionResult(BaseActionResult):
    # TODO: Change this to SessionData
    session_id: uuid.UUID

    # TODO: Add proper type
    result: Mapping[str, Any]

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