"""
标签分类 Schema（请求/响应模型）
"""
from typing import Optional, List
from pydantic import BaseModel, Field
from app.schemas.base import BaseModelSchema
from datetime import datetime


class TagCategoryBase(BaseModel):
    """标签分类基础模型"""
    parent_id: int = Field(0, description="父级分类ID（0表示根节点）")
    name: str = Field(..., min_length=1, max_length=100, description="分类名称")
    type: Optional[str] = Field("label", max_length=50, description="类型")
    sort_no: Optional[int] = Field(0, description="同级排序号（越小越靠前）")
    status: Optional[int] = Field(1, description="状态（1=启用，0=禁用）")


class TagCategoryCreate(TagCategoryBase):
    """创建标签分类请求"""
    pass


class TagCategoryUpdate(BaseModel):
    """更新标签分类请求"""
    parent_id: Optional[int] = None
    name: Optional[str] = Field(None, min_length=1, max_length=100)
    type: Optional[str] = Field(None, max_length=50)
    sort_no: Optional[int] = None
    status: Optional[int] = None


class TagCategoryResponse(BaseModelSchema):
    """标签分类响应模型"""
    id: int
    parent_id: int
    name: str
    parent_name: Optional[str] = None
    path: str
    level: int
    type: Optional[str] = None
    sort_no: int
    status: int
    is_deleted: int
    created_by: Optional[str] = None
    created_by_name: Optional[str] = None
    created_at: Optional[datetime] = None
    updated_by: Optional[str] = None
    updated_by_name: Optional[str] = None
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True
        json_encoders = {
            datetime: lambda v: v.strftime("%Y-%m-%d %H:%M:%S") if v else None
        }


class TagCategoryTreeResponse(BaseModelSchema):
    """标签分类树响应模型（与 TagCategoryResponse 结构一致，包含子节点）"""
    id: int
    parent_id: int
    name: str
    parent_name: Optional[str] = None
    path: str
    level: int
    type: Optional[str] = None
    sort_no: int
    status: int
    is_deleted: int
    created_by: Optional[str] = None
    created_by_name: Optional[str] = None
    created_at: Optional[datetime] = None
    updated_by: Optional[str] = None
    updated_by_name: Optional[str] = None
    updated_at: Optional[datetime] = None
    children: Optional[List['TagCategoryTreeResponse']] = None

    class Config:
        from_attributes = True
        json_encoders = {
            datetime: lambda v: v.strftime("%Y-%m-%d %H:%M:%S") if v else None
        }


class TagCategoryBatchDeleteRequest(BaseModel):
    """批量删除标签分类请求"""
    ids: List[int] = Field(..., description="分类ID列表")


class TagCategoryMoveRequest(BaseModel):
    """移动标签分类请求"""
    id: int = Field(..., description="分类ID")
    parent_id: int = Field(..., description="新的父级分类ID")
