from pydantic import BaseModel, Field,field_validator
from typing import Optional
from ..common.base import BaseResponseWithID
from ..common.datetime import datetime_date,get_current_date
from fastapi import HTTPException

class GeneratorUnitProject(BaseResponseWithID):
    project_id : Optional[str]= Field(None, description="项目ID")
    serial_number: Optional[int] = Field(None, description="机组序号")
    approval_status:Optional[str]=Field(None, description="审批状态")
    capacity: Optional[float] = Field(None, description="单机容量(MW)")
    plan_production_date: Optional[str] = Field(None, description="计划投产日期")
    actual_production_date: Optional[str] = Field(None, description="实际投产日期")

class PostGeneratorUnits(BaseModel):
    model_config = {"populate_by_name": True}

    id:str = Field(..., description="机组ID")
    project_id: str = Field(..., description="所属项目ID")
    actual_production_date: str = Field(..., alias="actual_date", description="实际投产日期(格式：YYYY-MM-DD)")

    @field_validator('actual_production_date', mode="before")
    def validate_actual_date_not_future(cls, v: str) -> str:
        # if v == "":
        #     return ""
        try:
            actual = datetime_date(v)
        except ValueError:
            raise HTTPException(status_code=400,detail='日期格式为(YYYY-MM-DD)')

        today = datetime_date(get_current_date())
        if actual > today:
            raise HTTPException(status_code=400,detail='机组日期不能晚于今天')
        return v