"""
资产分配相关的Pydantic Schema
"""
from typing import Optional, List, Dict, Any
from datetime import datetime, date
from decimal import Decimal
from pydantic import BaseModel, Field


# ===== 分配单Schema =====

class AllocationOrderBase(BaseModel):
    """分配单基础Schema"""
    order_type: str = Field(..., description="单据类型(allocation/transfer/recovery/maintenance/scrap)")
    title: str = Field(..., min_length=1, max_length=200, description="标题")
    source_organization_id: Optional[int] = Field(None, gt=0, description="调出网点ID")
    target_organization_id: int = Field(..., gt=0, description="调入网点ID")
    expect_execute_date: Optional[date] = Field(None, description="预计执行日期")
    remark: Optional[str] = Field(None, description="备注")


class AllocationOrderCreate(AllocationOrderBase):
    """创建分配单Schema"""
    asset_ids: List[int] = Field(..., min_items=1, description="资产ID列表")


class AllocationOrderUpdate(BaseModel):
    """更新分配单Schema"""
    title: Optional[str] = Field(None, min_length=1, max_length=200)
    expect_execute_date: Optional[date] = None
    remark: Optional[str] = None


class AllocationOrderApproval(BaseModel):
    """分配单审批Schema"""
    approval_status: str = Field(..., description="审批状态(approved/rejected)")
    approval_remark: Optional[str] = Field(None, description="审批备注")


class AllocationOrderExecute(BaseModel):
    """分配单执行Schema"""
    remark: Optional[str] = Field(None, description="执行备注")


class AllocationOrderInDB(BaseModel):
    """数据库中的分配单Schema"""
    id: int
    order_code: str
    order_type: str
    title: str
    source_organization_id: Optional[int]
    target_organization_id: int
    applicant_id: int
    approver_id: Optional[int]
    approval_status: str
    approval_time: Optional[datetime]
    approval_remark: Optional[str]
    expect_execute_date: Optional[date]
    actual_execute_date: Optional[date]
    executor_id: Optional[int]
    execute_status: str
    remark: Optional[str]
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class AllocationOrderResponse(AllocationOrderInDB):
    """分配单响应Schema"""
    pass


class AllocationOrderWithRelations(AllocationOrderResponse):
    """带关联信息的分配单响应Schema"""
    source_organization: Optional[Dict[str, Any]] = None
    target_organization: Optional[Dict[str, Any]] = None
    applicant: Optional[Dict[str, Any]] = None
    approver: Optional[Dict[str, Any]] = None
    executor: Optional[Dict[str, Any]] = None
    items: Optional[List[Dict[str, Any]]] = None


class AllocationOrderListResponse(BaseModel):
    """分配单列表响应Schema"""
    total: int
    page: int
    page_size: int
    total_pages: int
    items: List[AllocationOrderWithRelations]


# ===== 分配单明细Schema =====

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


class AllocationItemInDB(BaseModel):
    """数据库中的分配单明细Schema"""
    id: int
    order_id: int
    asset_id: int
    asset_code: str
    asset_name: str
    from_organization_id: Optional[int]
    to_organization_id: Optional[int]
    from_status: Optional[str]
    to_status: Optional[str]
    execute_status: str
    execute_time: Optional[datetime]
    failure_reason: Optional[str]
    remark: Optional[str]
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class AllocationItemResponse(AllocationItemInDB):
    """分配单明细响应Schema"""
    pass


# ===== 查询参数Schema =====

class AllocationOrderQueryParams(BaseModel):
    """分配单查询参数"""
    order_type: Optional[str] = Field(None, description="单据类型")
    approval_status: Optional[str] = Field(None, description="审批状态")
    execute_status: Optional[str] = Field(None, description="执行状态")
    applicant_id: Optional[int] = Field(None, gt=0, description="申请人ID")
    target_organization_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="每页数量")


# ===== 统计Schema =====

class AllocationOrderStatistics(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="已完成数")
