from datetime import datetime

from pydantic import BaseModel, ConfigDict, Field, field_validator


class DesktopConfigOut(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: int
    name: str
    value: str
    mark: str | None = None
    oem_id: int = 1
    created_at: datetime
    updated_at: datetime


class DesktopConfigCreate(BaseModel):
    name: str = Field(min_length=1, max_length=64)
    value: str = Field(min_length=0)
    mark: str | None = Field(default=None, max_length=255)
    oem_id: int = Field(default=1, ge=1)

    @field_validator("name")
    @classmethod
    def strip_name(cls, value: str) -> str:
        return value.strip()

    @field_validator("mark")
    @classmethod
    def normalize_mark(cls, value: str | None) -> str | None:
        if value is None:
            return None
        stripped = value.strip()
        return stripped or None


class DesktopConfigUpdate(BaseModel):
    name: str | None = Field(default=None, min_length=1, max_length=64)
    value: str | None = None
    mark: str | None = Field(default=None, max_length=255)

    @field_validator("name")
    @classmethod
    def strip_name(cls, value: str | None) -> str | None:
        if value is None:
            return None
        return value.strip()

    @field_validator("mark")
    @classmethod
    def normalize_mark(cls, value: str | None) -> str | None:
        if value is None:
            return None
        stripped = value.strip()
        return stripped or None
