from pydantic import BaseModel, ConfigDict, Field, field_validator

from app.services.contact_validation import normalize_customer_phone, normalize_optional_customer_email


class CustomerCreate(BaseModel):
    name: str = Field(min_length=1, max_length=120)
    phone: str = Field(min_length=3, max_length=40)
    email: str | None = None
    notes: str | None = None

    @field_validator("phone")
    @classmethod
    def validate_phone(cls, value: str) -> str:
        return normalize_customer_phone(value)

    @field_validator("email")
    @classmethod
    def validate_email(cls, value: str | None) -> str | None:
        return normalize_optional_customer_email(value)


class CustomerRead(BaseModel):
    id: int
    name: str
    phone: str
    email: str | None = None
    notes: str | None = None

    model_config = ConfigDict(from_attributes=True)
