"""
基础Schema模型
"""
from pydantic import BaseModel, Field
from typing import Optional, Any, Dict
from datetime import datetime


class BaseSchema(BaseModel):
    """基础Schema"""
    
    class Config:
        from_attributes = True
        json_encoders = {
            datetime: lambda v: v.isoformat() if v else None
        }


class ResponseSchema(BaseSchema):
    """统一响应Schema"""
    code: int = Field(default=0, description="响应码")
    message: str = Field(default="success", description="响应消息")
    data: Optional[Any] = Field(default=None, description="响应数据")
    timestamp: datetime = Field(default_factory=datetime.utcnow, description="响应时间")


class PaginationSchema(BaseSchema):
    """分页Schema"""
    page: int = Field(default=1, ge=1, description="页码")
    page_size: int = Field(default=20, ge=1, le=100, description="每页数量")
    total: int = Field(default=0, description="总数量")
    total_pages: int = Field(default=0, description="总页数")


class PaginatedResponseSchema(ResponseSchema):
    """分页响应Schema"""
    data: Optional[Any] = Field(default=None, description="响应数据")
    meta: Optional[PaginationSchema] = Field(default=None, description="分页信息")


class IDSchema(BaseSchema):
    """ID Schema"""
    id: str = Field(..., description="ID")


class TimestampSchema(BaseSchema):
    """时间戳Schema"""
    created_at: Optional[datetime] = Field(default=None, description="创建时间")
    updated_at: Optional[datetime] = Field(default=None, description="更新时间")


class BaseModelSchema(IDSchema, TimestampSchema):
    """基础模型Schema"""
    is_deleted: bool = Field(default=False, description="是否删除")


class PageQuery(BaseModel):
    """分页查询参数"""
    page: int = Field(1, ge=1)
    page_size: int = Field(20, ge=1, le=200)


class ApiResponse(BaseModel):
    code: int
    message: str
    data: Optional[Any] = None
    timestamp: str
