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


class RoleBase(BaseModel):
    """角色基础模式"""
    id: Optional[str] = None
    name: str = Field(..., description="角色名称")
    description: str = Field(default="", description="角色描述")


class RoleCreate(RoleBase):
    """创建角色模式"""
    pass


class RoleResponse(RoleBase):
    """角色响应模式"""
    created_at: datetime
    updated_at: Optional[datetime] = None


class UserBase(BaseModel):
    """用户基础模式"""
    id: Optional[str] = None
    username: str = Field(..., description="用户名")
    email: EmailStr = Field(..., description="邮箱")
    role_id: str = Field(..., description="角色ID")


class UserCreate(UserBase):
    """创建用户模式"""
    password: str = Field(..., description="密码")


class UserUpdate(BaseModel):
    """更新用户模式"""
    username: Optional[str] = None
    email: Optional[EmailStr] = None
    role_id: Optional[str] = None
    status: Optional[str] = None
    password: Optional[str] = None


class UserResponse(UserBase):
    """用户响应模式"""
    status: str
    created_at: datetime
    updated_at: Optional[datetime] = None
    role: Optional[RoleResponse] = None
    role_name: Optional[str] = None


class UserListResponse(BaseModel):
    """用户列表响应模式"""
    users: List[UserResponse]
    total: int


class Token(BaseModel):
    """令牌模式"""
    access_token: str
    token_type: str = "bearer"


class TokenData(BaseModel):
    """令牌数据模式"""
    username: Optional[str] = None
    user_id: Optional[str] = None


class LoginRequest(BaseModel):
    """登录请求模式"""
    username: str = Field(..., description="用户名")
    password: str = Field(..., description="密码")



