import uuid
from pydantic import BaseModel, Field, field_validator
import re

class UserCreate(BaseModel):
    username: str = Field(..., max_length=20, description='username of the user')
    phone_number: str | None = Field(None, description="11 digit phone number")
    is_admin: bool = Field(False)

    @field_validator("phone_number")
    @classmethod
    def validate_phone_number(cls, v: str | None) -> str | None:
        if v is None:
            return v
        if not re.match(r"^09\d{9}$", v):
            raise ValueError("شماره تلفن باید ۱۱ رقم باشد و با ۰۹ شروع شود")
        return v

class UserResponse(BaseModel):
    id: uuid.UUID = Field(...)
    username: str = Field(..., description='username of the user')
    phone_number: str | None = Field(..., description='phone number of the user')
    is_admin: bool = Field(..., description='is admin')
    is_active: bool = Field(..., description='is active')

    class Config:
        from_attributes = True

class UserCreateResult(BaseModel):
    user: UserResponse = Field(..., description='user created')
    secret: str = Field(..., description='secret of the user')
