from datetime import date, datetime

from pydantic import BaseModel, Field, field_validator


class CertificateCreate(BaseModel):
    learner_id: int
    project_code: str = Field(min_length=2, max_length=16)
    certificate_name: str | None = Field(default=None, max_length=128)
    class_name: str | None = Field(default=None, max_length=128)
    course_name: str | None = Field(default=None, max_length=128)
    stage_name: str | None = Field(default=None, max_length=128)
    issue_date: date
    issuer_name: str = Field(min_length=1, max_length=128)
    remark: str | None = None

    @field_validator("project_code")
    @classmethod
    def normalize_project_code(cls, value: str) -> str:
        return value.strip().upper()


class CertificateOut(BaseModel):
    id: int
    learner_id: int
    project_code: str
    certificate_no: str
    certificate_name: str
    class_name: str | None
    course_name: str | None
    stage_name: str | None
    issue_date: date
    issuer_name: str
    status: str
    pdf_status: str
    created_at: datetime
    updated_at: datetime

    model_config = {"from_attributes": True}
