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


class RoleOut(BaseModel):
    id: int
    name: str
    code: str
    description: Optional[str] = None

    class Config:
        from_attributes = True


class DeptOut(BaseModel):
    id: int
    name: str
    parent_id: Optional[int] = None
    sort_order: int = 0

    class Config:
        from_attributes = True


class DeptTree(DeptOut):
    children: List["DeptTree"] = []


class UserBase(BaseModel):
    username: str
    email: Optional[str] = None
    real_name: Optional[str] = None
    phone: Optional[str] = None
    dept_id: Optional[int] = None
    is_active: bool = True


class UserCreate(UserBase):
    password: str
    role_ids: List[int] = []


class UserUpdate(BaseModel):
    email: Optional[str] = None
    real_name: Optional[str] = None
    phone: Optional[str] = None
    dept_id: Optional[int] = None
    is_active: Optional[bool] = None
    role_ids: Optional[List[int]] = None


class UserOut(UserBase):
    id: int
    is_superuser: bool
    created_at: datetime
    dept: Optional[DeptOut] = None
    roles: List[RoleOut] = []

    class Config:
        from_attributes = True
