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

class UserBase(BaseModel):
    mobile: str
    name: Optional[str] = None
    english_name: Optional[str] = None
    status: str = "PENDING"
    role: Optional[str] = "ORDINARY_USER"
    is_deleted: int = 0

class UserCreate(UserBase):
    password: str
    admin_password: Optional[str] = None

class UserRegister(BaseModel):
    mobile: str
    password: str
    sms_code: str # Added field
    name: str # Chinese Name

class PromoteUserRequest(BaseModel):
    password: str
    captcha_id: str
    captcha_code: str

class BatchResetEnglishNameRequest(BaseModel):
    user_ids: List[int]
    admin_password: str

class UserUpdate(BaseModel):
    password: Optional[str] = None
    mobile: Optional[str] = None
    name: Optional[str] = None
    english_name: Optional[str] = None
    status: Optional[str] = None
    role: Optional[str] = None
    is_deleted: Optional[int] = None
    admin_password: Optional[str] = None

class UserSyncRequest(BaseModel):
    mobile: str
    name: Optional[str] = None
    english_name: Optional[str] = None
    password: Optional[str] = None
    status: Optional[str] = None
    mapped_key: Optional[str] = None # External User ID
    mapped_email: Optional[str] = None # External User Email
    is_active: Optional[bool] = None # True=Active, False=Disabled. None=No Change
    sync_action: Optional[str] = "UPSERT"

class UserInDBBase(UserBase):
    id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True

class User(UserInDBBase):
    pass

class UserInDB(UserInDBBase):
    password_hash: str

class UserList(BaseModel):
    total: int
    items: List[User]

class UserSyncSimple(BaseModel):
    mobile: str
    name: Optional[str] = None
    english_name: Optional[str] = None

    class Config:
        from_attributes = True

class UserSyncList(BaseModel):
    total: int
    items: List[UserSyncSimple]
