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
from ..common.validators import DateStr


class PostProduction(BaseModel):
    id: str = Field(..., description="投产节点ID")
    code: str = Field(..., description="节点编码")
    acquire_date: DateStr | None = Field(None, description="取得时间")
    is_exist: bool = Field(..., description="是否取得")

    @field_validator('acquire_date')
    def validate_acquire_date(cls, v: Optional[DateStr]) -> Optional[DateStr]:
        if v is None:
            return None
        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


class ProductionDetail(BaseResponseWithID):
    id: Optional[str] = Field(None, description="投产节点ID")
    project_id: Optional[str] = Field(None, description="项目ID")
    code: Optional[str] = Field(None, description="投产节点编码")
    acquire_date: Optional[DateStr] = Field(None, description="取得时间")
    is_exist: Optional[bool] = Field(None, description="是否取得")
    # approval_status: Optional[str] = Field(None, description="审批状态")
    name: Optional[str] = Field(None, description="投产节点名称")
    is_acquired: Optional[int] = Field(0, description="是否取得")
