"""统一返回类型定义"""
from typing import TypeVar, Generic, Optional, List, Any
from pydantic import BaseModel, Field

T = TypeVar('T')


class PageDomain(BaseModel, Generic[T]):
    """分页数据模型"""
    page: int = Field(default=1, description="当前页码")
    pageSize: int = Field(default=10, description="每页大小")
    total: int = Field(default=0, description="总记录数")
    dataList: Optional[List[T]] = Field(default=None, description="数据列表")


class ResultDomain(BaseModel, Generic[T]):
    """统一返回类型"""
    code: Optional[int] = Field(default=None, description="状态码")
    success: Optional[bool] = Field(default=None, description="是否成功")
    message: Optional[str] = Field(default=None, description="返回消息")
    data: Optional[T] = Field(default=None, description="单条数据")
    dataList: Optional[List[T]] = Field(default=None, description="数据列表")
    pageDomain: Optional[PageDomain[T]] = Field(default=None, description="分页数据")
    
    @staticmethod
    def ok(message: str = "success", data: Any = None) -> "ResultDomain":
        """成功返回 - 单条数据"""
        return ResultDomain(code=200, success=True, message=message, data=data)
    
    @staticmethod
    def ok_list(message: str = "success", data_list: List[Any] = None) -> "ResultDomain":
        """成功返回 - 数据列表"""
        return ResultDomain(code=200, success=True, message=message, dataList=data_list)
    
    @staticmethod
    def ok_page(message: str = "success", page_domain: "PageDomain" = None) -> "ResultDomain":
        """成功返回 - 分页数据"""
        result = ResultDomain(code=200, success=True, message=message, pageDomain=page_domain)
        if page_domain:
            result.dataList = page_domain.dataList
        return result
    
    @staticmethod
    def fail(message: str = "failure", code: int = 500) -> "ResultDomain":
        """失败返回"""
        return ResultDomain(code=code, success=False, message=message)
    
    model_config = {
        "json_schema_extra": {
            "examples": [{"code": 200, "success": True, "message": "操作成功"}]
        }
    }
