"""
contract_change相关schemea
"""
from ..common.validators import DateStr
from pydantic import BaseModel, Field
from typing import Optional
from ..common.base import BaseResponseWithID
from pydantic import field_validator
from fastapi import HTTPException

class ContractChangeResponse(BaseResponseWithID):
    """
    合同变更模型
    """
    id: Optional[str] = Field(default=None, description="合同变更ID")
    project_id : Optional[str] = Field(None, description="所属项目ID")
    contract_id: Optional[str] = Field(None, description="关联合同ID")
    name: Optional[str] = Field(None, description="合同名称")
    sub_name: Optional[str] = Field(None, description="合同子标题（epc合同和pc合同会有子标题）")
    sign_company:Optional[str] = Field(None, description="签约公司") 
    title: Optional[str] = Field(None, description="变更事项")
    reason: Optional[str] = Field(None, description="变更理由")
    before_amount: Optional[float] = Field(None, description="变更前金额")
    after_amount: Optional[float] = Field(None, description="变更后金额")
    change_count: Optional[float] = Field(None, description="变更金额")
    change_rate: Optional[str] = Field(None, description="变更比例")
    change_time: Optional[str] = Field(None, description="变更时间(格式：YYYY-MM-DD)")
    attachment_ids:Optional[list] = Field(None, description="附件ID列表")
    attachment_names:Optional[list] = Field(None, description="附件名称列表")
    created_at:Optional[str]=Field(None, description="创建时间")
    created_by_name: Optional[str] = Field(None, description="创建人名称")

class PostContractChange(BaseModel):
    """
    合同变更请求（水火抽蓄气电）
    PROJECT_CONTRACT_CHANGE = "project.contract-change"
    """


    id: str  = Field(..., description="合同变更ID，更新时传入，创建时不传")
    project_id: str = Field(..., description="所属项目ID")
    contract_id: str = Field(..., description="合同ID")
    title: str = Field(..., description="变更事项")
    reason: str = Field(..., description="变更理由")
    before_amount: float = Field(..., description="变更前金额")
    after_amount: float = Field(..., description="变更后金额")
    change_time: DateStr = Field(..., description="变更时间(格式：YYYY-MM-DD)")
    attachment_ids: list = Field(..., description="附件ID列表")

    @field_validator("after_amount")
    def validate_after_amount(cls, v: float) -> float:
        if v < 0:
            raise HTTPException(status_code=400, detail="变更后金额必须小于等于0")
        return v

class CreateContractChange(BaseModel):
        """
        创建合同变更请求（水火抽蓄气电）
        PROJECT_CONTRACT_CHANGE = "project.contract-change"
        """

        project_id: str = Field(..., description="所属项目ID")
        contract_id: str = Field(..., description="合同ID")
        title: str = Field(..., description="变更事项")
        reason: str = Field(..., description="变更理由")
        before_amount: float = Field(..., description="变更前金额")
        after_amount: float = Field(..., description="变更后金额")
        change_time: DateStr = Field(..., description="变更时间(格式：YYYY-MM-DD)")
        attachment_ids: list = Field(..., description="附件ID列表")

        @field_validator("after_amount")
        def validate_after_amount(cls, v: float) -> float:
            if v < 0:
                raise HTTPException(status_code=400, detail="变更后金额不能小于0")
            return v
