"""
移民征地相关schema
"""

from pydantic import BaseModel, Field, field_validator
from typing import Optional, List
from ..common.base import BaseResponseWithID
from ..common.validators import DateStr
from ..common.datetime import get_current_date


class LarDetailSchema(BaseResponseWithID):
    id: str = Field(..., description="移民征地id")
    project_id: Optional[str] = Field(None, description="项目ID")
    plan_resettlement_pop: Optional[int] = Field(None, description="计划搬迁人数")
    completed_resettlement_pop: Optional[int] = Field(None, description="完成搬迁人数")

    plan_investment_amt: Optional[float] = Field(None, description="计划投资金额 ")
    completed_investment_amt: Optional[float] = Field(None, description="完成投资金额 ")

    plan_urban_comp: Optional[float] = Field(None, description="计划城镇补偿金额 ")
    actual_urban_comp: Optional[float] = Field(None, description="实际城镇补偿金额 ")

    plan_rural_comp: Optional[float] = Field(None, description="计划农村补偿金额 ")
    actual_rural_comp: Optional[float] = Field(None, description="实际农村补偿金额 ")

    plan_spec_project_reconstr: Optional[float] = Field(None, description="计划专项工程复建 ")
    actual_spec_project_reconstr: Optional[float] = Field(None, description="实际专项工程复建 ")

    plan_reservoir_bottom_cleanup: Optional[float] = Field(None, description="计划库底清理 ")
    actual_reservoir_bottom_cleanup: Optional[float] = Field(None, description="实际库底清理 ")


class LarMainInfoUpdateSchema(BaseResponseWithID):
    id: str = Field(..., description="移民征地id")
    project_id: str = Field(..., description="项目id")

    plan_urban_comp: float = Field(..., description="计划城镇补偿金额 ")
    actual_urban_comp: float = Field(..., description="实际城镇补偿金额 ")

    plan_rural_comp: float = Field(..., description="计划农村补偿金额 ")
    actual_rural_comp: float = Field(..., description="实际农村补偿金额 ")

    plan_spec_project_reconstr: float = Field(..., description="计划专项工程复建 ")
    actual_spec_project_reconstr: float = Field(..., description="实际专项工程复建 ")

    plan_reservoir_bottom_cleanup: float = Field(..., description="计划库底清理 ")
    actual_reservoir_bottom_cleanup: float = Field(..., description="实际库底清理 ")

    @field_validator(
        "plan_urban_comp",
        "actual_urban_comp",
        "plan_rural_comp",
        "actual_rural_comp",
        "plan_spec_project_reconstr",
        "actual_spec_project_reconstr",
        "plan_reservoir_bottom_cleanup",
        "actual_reservoir_bottom_cleanup",
        mode="before",
    )
    def check_amount_field(cls, v):
        from fastapi import HTTPException
        if v is None or v == "":
            raise HTTPException(detail='金额字段不能为空', status_code=400)
        if float(v) < 0:
            raise HTTPException(detail='金额字段需大于等于0', status_code=400)
        return v


class LarResettlementSchema(BaseResponseWithID):
    id: str = Field(..., description="移民征地id")
    project_id: str = Field(..., description="项目id")

    plan_resettlement_pop: int = Field(..., description="计划搬迁人数")
    completed_resettlement_pop: int = Field(..., description="完成搬迁人数")

    @field_validator(
        "plan_resettlement_pop",
        "completed_resettlement_pop",
        mode="before",
    )
    def check_amount_field(cls, v):
        from fastapi import HTTPException
        if v is None or v == "":
            raise HTTPException(detail='搬迁人数不能为空', status_code=400)
        if float(v) < 0:
            raise HTTPException(detail='搬迁人数需大于等于0', status_code=400)
        return v


class LarInvestmentSchema(BaseResponseWithID):
    id: str = Field(..., description="移民征地id")
    project_id: str = Field(..., description="项目id")

    plan_investment_amt: float = Field(..., description="计划投资金额")
    completed_investment_amt: float = Field(..., description="完成投资金额")

    @field_validator(
        "plan_investment_amt",
        "completed_investment_amt",
        mode="before",
    )
    def check_amount_field(cls, v):
        from fastapi import HTTPException
        if v is None or v == "":
            raise HTTPException(detail='金额字段不能为空', status_code=400)
        if float(v) < 0:
            raise HTTPException(detail='金额字段数需大于等于0', status_code=400)
        return v


class LarChangeDetailSchema(BaseResponseWithID):
    project_id: str = Field(..., description="项目ID")
    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_time: Optional[str] = Field(None, description="变更时间")
    attachment_ids: List[str] = Field(default_factory=list, description="附件ID列表", examples=[[]])

    created_at: Optional[str] = Field(None, description="创建时间")
    created_by_name: Optional[str] = Field(None, description="创建人")

    change_amount: Optional[float] = Field(None, description="变更金额")
    change_percentage: Optional[str] = Field('', description="变更比例")

    approval_status: Optional[str] = Field(None, description="审批状态")

    attachment_info: List[dict] = Field(
        default_factory=list, description="文件信息", examples=[[
            {
                "id": "54a0daf903a04c8e87cea1175458459d",
                "name": "54a0daf903a04c8e87cea1175458459d.pdf",
                "original_name": "新用户注册申请表-项目公司.pdf",
                "url": "/api/file_download/54a0daf903a04c8e87cea1175458459d"
            }
        ]]
    )


class LarChangeInfoSchema(BaseResponseWithID):
    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_time: Optional[str] = Field(None, description="变更时间")
    attachment_ids: List[str] = Field(default_factory=list, description="附件ID列表", examples=[[]])

    approval_status: Optional[str] = Field(None, description="审批状态")

    before_amount_is_edit: Optional[bool] = Field(None, description="变更前金额是否可编辑")

    changed_count: Optional[int] = Field(None, description="已办更次数")


class LarChangeListSchema(BaseModel):
    page: int
    page_size: int
    total: int
    list: List[LarChangeDetailSchema]


class LarChangeCreateSchema(BaseModel):
    title: str = Field(..., description="变更事项")
    reason: str = Field(..., description="变更理由")

    before_amount: float = Field(..., description="变更前金额")
    after_amount: float = Field(..., description="变更后金额")

    change_time: DateStr = Field(..., description="变更时间")
    attachment_ids: List[str] = Field(default_factory=list, description="附件ID列表", examples=[[]])

    project_id: str = Field(..., description="项目id")

    @field_validator(
        "before_amount",
        "after_amount",
        mode="before",
    )
    def check_amount_field(cls, v):
        from fastapi import HTTPException
        if v is None or v == "":
            raise HTTPException(detail='金额字段不能为空', status_code=400)
        if float(v) < 0:
            raise HTTPException(detail='金额字段需大于等于0', status_code=400)
        return v

    @field_validator(
        "change_time",
        mode="before",
    )
    def check_actual_time_field(cls, v):
        from fastapi import HTTPException
        if v is None or v == "":
            raise HTTPException(detail='移民变更时间必填', status_code=400)
        if v > get_current_date():
            raise HTTPException(detail='移民变更时间不能大于当前日期', status_code=400)
        return v


class LarChangeUpdateSchema(LarChangeCreateSchema):
    id: str = Field(..., description="移民变更id")
