from pydantic import BaseModel, Field
from typing import Optional, Generic, TypeVar, Any, List

# 定义一个泛型类型变量 T
T = TypeVar("T")


# 使用泛型 T 定义 data 的类型
class StandardResponse(BaseModel, Generic[T]):
    code: int = 200
    data: Optional[T] = None
    message: str = "请求成功"
    success: bool = True


class PageResponse(BaseModel, Generic[T]):
    total: int
    items: List[T] = Field(default_factory=list)


def standard_response(data: Any = None, code: int = 200, message: str = "请求成功", success: bool = True):
    return StandardResponse(data=data, code=code, message=message, success=success)


def standard_error_response(data: Any = None, code: int = 500, message: str = "请求异常", success: bool = False):
    return StandardResponse(data=data, code=code, message=message, success=success)


def convert_page_param(offset: int, limit: int):
    offset = (offset - 1) * limit
    limit = limit
    return offset, limit
