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


# ===== 回收单Schema =====

class AssetRecoveryOrderBase(BaseModel):
    """回收单基础Schema"""
    recovery_type: str = Field(..., description="回收类型(user=使用人回收/org=机构回收/scrap=报废回收)")
    title: str = Field(..., min_length=1, max_length=200, description="标题")
    remark: Optional[str] = Field(None, description="备注")


class AssetRecoveryOrderCreate(AssetRecoveryOrderBase):
    """创建回收单Schema"""
    asset_ids: List[int] = Field(..., min_items=1, description="资产ID列表")


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


class AssetRecoveryOrderInDB(BaseModel):
    """数据库中的回收单Schema"""
    id: int
    order_code: str
    recovery_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 AssetRecoveryOrderResponse(AssetRecoveryOrderInDB):
    """回收单响应Schema"""
    pass


class AssetRecoveryOrderWithRelations(AssetRecoveryOrderResponse):
    """带关联信息的回收单响应Schema"""
    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 AssetRecoveryOrderQueryParams(BaseModel):
    """回收单查询参数"""
    recovery_type: Optional[str] = Field(None, description="回收类型")
    approval_status: Optional[str] = Field(None, description="审批状态")
    execute_status: Optional[str] = Field(None, description="执行状态")
    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 AssetRecoveryOrderListResponse(BaseModel):
    """回收单列表响应Schema"""
    total: int
    page: int
    page_size: int
    total_pages: int
    items: List[AssetRecoveryOrderWithRelations]


class AssetRecoveryStatistics(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 AssetRecoveryItemBase(BaseModel):
    """回收单明细基础Schema"""
    asset_id: int = Field(..., gt=0, description="资产ID")
    remark: Optional[str] = Field(None, description="备注")


class AssetRecoveryItemInDB(BaseModel):
    """数据库中的回收单明细Schema"""
    id: int
    order_id: int
    asset_id: int
    asset_code: str
    recovery_status: str
    created_at: datetime

    class Config:
        from_attributes = True


class AssetRecoveryItemResponse(AssetRecoveryItemInDB):
    """回收单明细响应Schema"""
    pass
