"""
资产调拨相关的Pydantic Schema
"""
from typing import Optional, List, Dict, Any
from datetime import datetime
from pydantic import BaseModel, Field


# ===== 调拨单Schema =====

class AssetTransferOrderBase(BaseModel):
    """调拨单基础Schema"""
    source_org_id: int = Field(..., gt=0, description="调出网点ID")
    target_org_id: int = Field(..., gt=0, description="调入网点ID")
    transfer_type: str = Field(..., description="调拨类型(internal=内部调拨/external=跨机构调拨)")
    title: str = Field(..., min_length=1, max_length=200, description="标题")
    remark: Optional[str] = Field(None, description="备注")


class AssetTransferOrderCreate(AssetTransferOrderBase):
    """创建调拨单Schema"""
    asset_ids: List[int] = Field(..., min_items=1, description="资产ID列表")


class AssetTransferOrderUpdate(BaseModel):
    """更新调拨单Schema"""
    title: Optional[str] = Field(None, min_length=1, max_length=200, description="标题")
    remark: Optional[str] = Field(None, description="备注")


class AssetTransferOrderStart(BaseModel):
    """开始调拨Schema"""
    remark: Optional[str] = Field(None, description="开始备注")


class AssetTransferOrderComplete(BaseModel):
    """完成调拨Schema"""
    remark: Optional[str] = Field(None, description="完成备注")


class AssetTransferOrderInDB(BaseModel):
    """数据库中的调拨单Schema"""
    id: int
    order_code: str
    source_org_id: int
    target_org_id: int
    transfer_type: str
    title: str
    asset_count: int
    apply_user_id: int
    apply_time: datetime
    approval_status: str
    approval_user_id: Optional[int]
    approval_time: Optional[datetime]
    approval_remark: Optional[str]
    execute_status: str
    execute_user_id: Optional[int]
    execute_time: Optional[datetime]
    remark: Optional[str]
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class AssetTransferOrderResponse(AssetTransferOrderInDB):
    """调拨单响应Schema"""
    pass


class AssetTransferOrderWithRelations(AssetTransferOrderResponse):
    """带关联信息的调拨单响应Schema"""
    source_organization: Optional[Dict[str, Any]] = None
    target_organization: Optional[Dict[str, Any]] = None
    apply_user: Optional[Dict[str, Any]] = None
    approval_user: Optional[Dict[str, Any]] = None
    execute_user: Optional[Dict[str, Any]] = None
    items: Optional[List[Dict[str, Any]]] = None


class AssetTransferOrderQueryParams(BaseModel):
    """调拨单查询参数"""
    transfer_type: Optional[str] = Field(None, description="调拨类型")
    approval_status: Optional[str] = Field(None, description="审批状态")
    execute_status: Optional[str] = Field(None, description="执行状态")
    source_org_id: Optional[int] = Field(None, gt=0, description="调出网点ID")
    target_org_id: Optional[int] = Field(None, gt=0, description="调入网点ID")
    keyword: Optional[str] = Field(None, description="搜索关键词")
    page: int = Field(default=1, ge=1, description="页码")
    page_size: int = Field(default=20, ge=1, le=100, description="每页数量")


class AssetTransferOrderListResponse(BaseModel):
    """调拨单列表响应Schema"""
    total: int
    page: int
    page_size: int
    total_pages: int
    items: List[AssetTransferOrderWithRelations]


class AssetTransferStatistics(BaseModel):
    """调拨单统计Schema"""
    total: int = Field(..., description="总数")
    pending: int = Field(..., description="待审批数")
    approved: int = Field(..., description="已审批数")
    rejected: int = Field(..., description="已拒绝数")
    executing: int = Field(..., description="执行中数")
    completed: int = Field(..., description="已完成数")


# ===== 调拨单明细Schema =====

class AssetTransferItemBase(BaseModel):
    """调拨单明细基础Schema"""
    asset_id: int = Field(..., gt=0, description="资产ID")
    remark: Optional[str] = Field(None, description="备注")


class AssetTransferItemInDB(BaseModel):
    """数据库中的调拨单明细Schema"""
    id: int
    order_id: int
    asset_id: int
    asset_code: str
    source_organization_id: int
    target_organization_id: int
    transfer_status: str
    created_at: datetime

    class Config:
        from_attributes = True


class AssetTransferItemResponse(AssetTransferItemInDB):
    """调拨单明细响应Schema"""
    pass
