"""
ERP 物流域 Pydantic V2 Schemas
发货单创建（嵌套明细）/ 列表 / 详情
"""

from __future__ import annotations

import uuid
from datetime import date, datetime

from pydantic import BaseModel, Field


# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 发货明细创建
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
class ShippingItemCreate(BaseModel):
    order_item_id: uuid.UUID = Field(..., description="关联的订单明细行 ID")
    sku_id: uuid.UUID = Field(..., description="产品 SKU ID")
    shipped_qty: float = Field(..., gt=0, description="本次发货数量，必须 > 0")


# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 发货单创建
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
class ShippingCreate(BaseModel):
    order_id: uuid.UUID = Field(..., description="关联订单 ID")
    carrier: str | None = Field(default=None, max_length=100, examples=["德邦物流"])
    tracking_no: str | None = Field(default=None, max_length=100, examples=["DB202602270001"])
    ship_date: date | None = None
    remark: str | None = None
    items: list[ShippingItemCreate] = Field(..., min_length=1, description="至少一个发货明细行")


# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 发货明细响应
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
class ShippingItemResponse(BaseModel):
    id: uuid.UUID
    order_item_id: uuid.UUID
    sku_id: uuid.UUID
    sku_code: str | None = None
    sku_name: str | None = None
    spec: str | None = None
    unit: str | None = None
    shipped_qty: float

    model_config = {"from_attributes": True}


# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 发货单响应
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
class ShippingResponse(BaseModel):
    id: uuid.UUID
    shipping_no: str
    order_id: uuid.UUID
    order_no: str | None = None
    customer_name: str | None = None
    carrier: str | None = None
    tracking_no: str | None = None
    status: str
    ship_date: date
    remark: str | None = None
    operator_name: str | None = None
    items: list[ShippingItemResponse] = Field(default_factory=list)
    created_at: datetime

    model_config = {"from_attributes": True}


# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 发货列表（Brief，不带明细减少体积）
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
class ShippingBriefResponse(BaseModel):
    id: uuid.UUID
    shipping_no: str
    order_id: uuid.UUID
    order_no: str | None = None
    customer_name: str | None = None
    carrier: str | None = None
    tracking_no: str | None = None
    status: str
    ship_date: date
    operator_name: str | None = None
    created_at: datetime

    model_config = {"from_attributes": True}


class ShippingListResponse(BaseModel):
    total: int
    items: list[ShippingBriefResponse]
    page: int
    size: int
