"""
品牌和供应商相关的Pydantic Schema
"""
from typing import Optional
from datetime import datetime
from pydantic import BaseModel, Field, EmailStr


# ===== 品牌Schema =====

class BrandBase(BaseModel):
    """品牌基础Schema"""
    brand_code: str = Field(..., min_length=1, max_length=50, description="品牌代码")
    brand_name: str = Field(..., min_length=1, max_length=200, description="品牌名称")
    logo_url: Optional[str] = Field(None, max_length=500, description="Logo URL")
    website: Optional[str] = Field(None, max_length=500, description="官网地址")
    sort_order: int = Field(default=0, description="排序")


class BrandCreate(BrandBase):
    """创建品牌Schema"""
    pass


class BrandUpdate(BaseModel):
    """更新品牌Schema"""
    brand_name: Optional[str] = Field(None, min_length=1, max_length=200)
    logo_url: Optional[str] = Field(None, max_length=500)
    website: Optional[str] = Field(None, max_length=500)
    status: Optional[str] = Field(None, pattern="^(active|inactive)$")
    sort_order: Optional[int] = None


class BrandInDB(BaseModel):
    """数据库中的品牌Schema"""
    id: int
    brand_code: str
    brand_name: str
    logo_url: Optional[str]
    website: Optional[str]
    status: str
    sort_order: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class BrandResponse(BrandInDB):
    """品牌响应Schema"""
    pass


# ===== 供应商Schema =====

class SupplierBase(BaseModel):
    """供应商基础Schema"""
    supplier_code: str = Field(..., min_length=1, max_length=50, description="供应商代码")
    supplier_name: str = Field(..., min_length=1, max_length=200, description="供应商名称")
    contact_person: Optional[str] = Field(None, max_length=100, description="联系人")
    contact_phone: Optional[str] = Field(None, max_length=20, description="联系电话")
    email: Optional[EmailStr] = Field(None, description="邮箱")
    address: Optional[str] = Field(None, max_length=500, description="地址")
    credit_code: Optional[str] = Field(None, max_length=50, description="统一社会信用代码")
    bank_name: Optional[str] = Field(None, max_length=200, description="开户银行")
    bank_account: Optional[str] = Field(None, max_length=100, description="银行账号")
    remark: Optional[str] = Field(None, description="备注")


class SupplierCreate(SupplierBase):
    """创建供应商Schema"""
    pass


class SupplierUpdate(BaseModel):
    """更新供应商Schema"""
    supplier_name: Optional[str] = Field(None, min_length=1, max_length=200)
    contact_person: Optional[str] = Field(None, max_length=100)
    contact_phone: Optional[str] = Field(None, max_length=20)
    email: Optional[EmailStr] = None
    address: Optional[str] = Field(None, max_length=500)
    credit_code: Optional[str] = Field(None, max_length=50)
    bank_name: Optional[str] = Field(None, max_length=200)
    bank_account: Optional[str] = Field(None, max_length=100)
    status: Optional[str] = Field(None, pattern="^(active|inactive)$")
    remark: Optional[str] = None


class SupplierInDB(BaseModel):
    """数据库中的供应商Schema"""
    id: int
    supplier_code: str
    supplier_name: str
    contact_person: Optional[str]
    contact_phone: Optional[str]
    email: Optional[str]
    address: Optional[str]
    credit_code: Optional[str]
    bank_name: Optional[str]
    bank_account: Optional[str]
    status: str
    remark: Optional[str]
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class SupplierResponse(SupplierInDB):
    """供应商响应Schema"""
    pass
