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

T = TypeVar("T")


class Response(BaseModel, Generic[T]):
    code: int = 200
    msg: str = "success"
    data: Optional[T] = None

    @classmethod
    def ok(cls, data=None, msg="success"):
        return cls(code=200, msg=msg, data=data)

    @classmethod
    def error(cls, msg="error", code=500):
        return cls(code=code, msg=msg, data=None)


class PageParams(BaseModel):
    page: int = 1
    page_size: int = 20


class PageResponse(BaseModel, Generic[T]):
    total: int
    page: int
    page_size: int
    items: List[T]
