# ================================================================
# services/auth/app/schemas/users.py
# ================================================================
"""
User schemas
"""

from pydantic import BaseModel, EmailStr, Field, validator
from typing import Optional, List
from datetime import datetime

from shared.utils.validation import validate_spanish_phone

class UserUpdate(BaseModel):
    """User update schema"""
    full_name: Optional[str] = Field(None, min_length=2, max_length=100)
    phone: Optional[str] = None
    language: Optional[str] = Field(None, pattern="^(es|en)$")
    timezone: Optional[str] = None

    @validator('phone')
    def validate_phone(cls, v):
        """Validate phone number"""
        if v and not validate_spanish_phone(v):
            raise ValueError('Invalid Spanish phone number')
        return v

class UserProfile(BaseModel):
    """User profile schema"""
    id: str
    email: str
    full_name: str
    phone: Optional[str]
    language: str
    timezone: str
    is_active: bool
    is_verified: bool
    created_at: datetime
    last_login: Optional[datetime]

    class Config:
        from_attributes = True

class BatchUserRequest(BaseModel):
    """Request schema for batch user fetch"""
    user_ids: List[str] = Field(..., description="List of user IDs to fetch", min_items=1, max_items=100)

class OwnerUserCreate(BaseModel):
    """Schema for owner-created users (pilot phase)"""
    email: EmailStr = Field(..., description="User email address")
    full_name: str = Field(..., min_length=2, max_length=100, description="Full name of the user")
    password: str = Field(..., min_length=8, max_length=128, description="Initial password for the user")
    phone: Optional[str] = Field(None, description="Phone number")
    role: str = Field("user", pattern="^(user|admin|manager)$", description="User role in the system")
    language: Optional[str] = Field("es", pattern="^(es|en|eu)$", description="Preferred language")
    timezone: Optional[str] = Field("Europe/Madrid", description="User timezone")

    @validator('phone')
    def validate_phone_number(cls, v):
        """Validate phone number"""
        if v and not validate_spanish_phone(v):
            raise ValueError('Invalid Spanish phone number format')
        return v
