from dataclasses import dataclass
from typing import Any, Generic

from typing_extensions import TypeVar

from app.constants.api_result import ApiCode

T = TypeVar("T")


@dataclass
class ApiResult(Generic[T]):
    code: int
    message: str
    success: bool
    data: T | None = None

    @staticmethod
    def ok(data: T | None = None) -> 'ApiResult[T]':
        return ApiResult(code=ApiCode.OK.value, message="ok", success=True, data=data)

    @staticmethod
    def error(code: int, message: str) -> 'ApiResult[None]':
        return ApiResult(code=code, message=message, success=False, data=None)
