from enum import StrEnum
from typing import Any


class ErrorCode(StrEnum):
    IMAGE_PULL_FAILED = "IMAGE_PULL_FAILED"
    DOCKER_DAEMON_UNAVAILABLE = "DOCKER_DAEMON_UNAVAILABLE"
    CONTAINER_CREATE_FAILED = "CONTAINER_CREATE_FAILED"
    CONTAINER_START_FAILED = "CONTAINER_START_FAILED"
    CONTAINER_NOT_FOUND = "CONTAINER_NOT_FOUND"
    INSUFFICIENT_RUNTIME_PERMISSIONS = "INSUFFICIENT_RUNTIME_PERMISSIONS"
    LOGIN_FAILED = "LOGIN_FAILED"
    BACKEND_ERROR = "BACKEND_ERROR"


class RunnerError(Exception):
    def __init__(
        self,
        code: ErrorCode,
        message: str,
        details: dict[str, Any] | None = None,
    ) -> None:
        super().__init__(message)
        self.code = code
        self.message = message
        self.details = details

    def to_dict(self) -> dict[str, Any]:
        return {
            "code": self.code.value,
            "message": self.message,
            "details": self.details,
        }
