from pydantic import BaseModel, EmailStr, field_validator
from typing import Optional
import uuid
import re

class UserBase(BaseModel):
    email: EmailStr
    username: str
    full_name: str
    phone: str

class UserCreate(UserBase):
    password: str
    
    @field_validator('phone')
    @classmethod
    def validate_phone(cls, v):
        # Remove tudo que não é número
        phone = re.sub(r'\D', '', v)
        if len(phone) < 10 or len(phone) > 11:
            raise ValueError('Telefone deve ter 10 ou 11 dígitos')
        return phone
    
    @field_validator('full_name')
    @classmethod
    def validate_full_name(cls, v):
        if not v or len(v.strip()) < 3:
            raise ValueError('Nome completo deve ter no mínimo 3 caracteres')
        return v.strip()

class UserLogin(BaseModel):
    email: EmailStr
    password: str

class UserResponse(BaseModel):
    id: uuid.UUID
    email: EmailStr
    username: str
    full_name: Optional[str]
    phone: Optional[str]
    is_superadmin: bool = False
    is_verified: bool = False
    
    class Config:
        from_attributes = True

class Token(BaseModel):
    access_token: str
    token_type: str = "bearer"
    user: UserResponse

class TokenData(BaseModel):
    sub: str
