from pydantic import BaseModel, Field, field_validator
from typing import List, Optional, Literal
from ..common.base import BaseResponseWithID


class CompanyResponse(BaseResponseWithID):
    code: str = Field(..., description="公司编码")
    company_type: str = Field(..., description="公司类型")
    name: str = Field(..., description="公司名称")
    short_name: str = Field(..., description="公司简称")
    parent_id: Optional[str] = Field(None, description="父公司ID")
    path: List[str] = Field(default_factory=list, description="公司路径 [..., 上级id, 本身id]")
    path_name: List[str] = Field(default_factory=list, description="公司路径名称 [..., 上级名称, 本身名称]")
    province: Optional[str] = Field(None, description="省份编码")  # 存省份编码
    city: Optional[str] = Field(None, description="城市编码")  # 存城市编码
    sort: int = Field(0, description="排序")
    status: str = Field("", description="公司状态")
    company_nature: Optional[list] = Field(..., description="公司性质")
    credit_code: Optional[str] = Field(None, description="信用代码", examples=["914403007109310121"])

    city_name: Optional[str] = Field(None, description="城市名称", examples=[None])
    province_name: Optional[str] = Field(None, description="省份名称", examples=[None])
    parent_name: Optional[str] = Field(None, description="父级公司名称", examples=[None])

    @field_validator(
        "path",
        "path_name",
        mode="before"
    )
    def formate_null_to_list(cls, field_value):
        if field_value is None:
            return []
        return field_value


class CompanyListResponse(BaseModel):
    page: int
    page_size: int
    total: int
    list: List[CompanyResponse] = []
