"""
用户相关Schema模型
"""
from pydantic import BaseModel, Field, EmailStr, field_validator
from typing import Optional, List, Dict, Any
from datetime import datetime, date
from .base import BaseSchema, BaseModelSchema


class UserBase(BaseSchema):
    """用户基础Schema"""
    username: str = Field(..., min_length=3, max_length=50, description="用户名")
    email: EmailStr = Field(..., description="邮箱")
    phone: Optional[str] = Field(None, max_length=20, description="手机号")
    avatar_url: Optional[str] = Field(None, max_length=500, description="头像URL")


class UserCreate(UserBase):
    """创建用户Schema"""
    password: str = Field(..., min_length=8, max_length=128, description="密码")
    
    @field_validator("password")
    @classmethod
    def validate_password(cls, v):
        """密码验证"""
        if not any(c.isupper() for c in v):
            raise ValueError("密码必须包含至少一个大写字母")
        if not any(c.islower() for c in v):
            raise ValueError("密码必须包含至少一个小写字母")
        if not any(c.isdigit() for c in v):
            raise ValueError("密码必须包含至少一个数字")
        return v


class UserUpdate(BaseSchema):
    """更新用户Schema"""
    email: Optional[EmailStr] = Field(None, description="邮箱")
    phone: Optional[str] = Field(None, max_length=20, description="手机号")
    avatar_url: Optional[str] = Field(None, max_length=500, description="头像URL")


class UserPasswordUpdate(BaseSchema):
    """更新密码Schema"""
    old_password: str = Field(..., description="旧密码")
    new_password: str = Field(..., min_length=8, max_length=128, description="新密码")
    
    @field_validator("new_password")
    @classmethod
    def validate_password(cls, v):
        """密码验证"""
        if not any(c.isupper() for c in v):
            raise ValueError("密码必须包含至少一个大写字母")
        if not any(c.islower() for c in v):
            raise ValueError("密码必须包含至少一个小写字母")
        if not any(c.isdigit() for c in v):
            raise ValueError("密码必须包含至少一个数字")
        return v


class UserStatusUpdate(BaseSchema):
    """更新用户状态Schema"""
    is_active: bool = Field(..., description="是否激活")


class UserResponse(UserBase, BaseModelSchema):
    """用户响应Schema"""
    is_active: bool = Field(..., description="是否激活")
    is_superuser: bool = Field(..., description="是否超级管理员")
    last_login_at: Optional[datetime] = Field(None, description="最后登录时间")
    last_login_ip: Optional[str] = Field(None, description="最后登录IP")
    failed_login_attempts: int = Field(..., description="失败登录次数")
    locked_until: Optional[datetime] = Field(None, description="锁定直到时间")


class UserProfileBase(BaseSchema):
    """用户详情基础Schema"""
    real_name: Optional[str] = Field(None, max_length=50, description="真实姓名")
    gender: Optional[int] = Field(None, ge=0, le=2, description="性别 0:未知 1:男 2:女")
    birth_date: Optional[date] = Field(None, description="出生日期")
    address: Optional[str] = Field(None, max_length=500, description="地址")
    company: Optional[str] = Field(None, max_length=100, description="公司")
    department: Optional[str] = Field(None, max_length=100, description="部门")
    position: Optional[str] = Field(None, max_length=100, description="职位")
    extra_info: Optional[Dict[str, Any]] = Field(None, description="扩展信息")


class UserProfileCreate(UserProfileBase):
    """创建用户详情Schema"""
    user_id: str = Field(..., description="用户ID")


class UserProfileUpdate(UserProfileBase):
    """更新用户详情Schema"""
    pass


class UserProfileResponse(UserProfileBase, BaseModelSchema):
    """用户详情响应Schema"""
    user_id: str = Field(..., description="用户ID")


class UserWithProfileResponse(UserResponse):
    """用户及详情响应Schema"""
    profile: Optional[UserProfileResponse] = Field(None, description="用户详情")


class RoleBase(BaseSchema):
    """角色基础Schema"""
    name: str = Field(..., max_length=50, description="角色名称")
    code: str = Field(..., max_length=50, description="角色代码")
    description: Optional[str] = Field(None, max_length=500, description="角色描述")


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


class RoleUpdate(BaseSchema):
    """更新角色Schema"""
    name: Optional[str] = Field(None, max_length=50, description="角色名称")
    description: Optional[str] = Field(None, max_length=500, description="角色描述")
    is_active: Optional[bool] = Field(None, description="是否激活")


class RoleResponse(RoleBase, BaseModelSchema):
    """角色响应Schema"""
    is_active: bool = Field(..., description="是否激活")


class PermissionBase(BaseSchema):
    """权限基础Schema"""
    name: str = Field(..., max_length=100, description="权限名称")
    code: str = Field(..., max_length=100, description="权限代码")
    description: Optional[str] = Field(None, max_length=500, description="权限描述")
    resource: Optional[str] = Field(None, max_length=100, description="资源")
    action: Optional[str] = Field(None, max_length=50, description="操作")


class PermissionCreate(PermissionBase):
    """创建权限Schema"""
    pass


class PermissionUpdate(BaseSchema):
    """更新权限Schema"""
    name: Optional[str] = Field(None, max_length=100, description="权限名称")
    description: Optional[str] = Field(None, max_length=500, description="权限描述")
    resource: Optional[str] = Field(None, max_length=100, description="资源")
    action: Optional[str] = Field(None, max_length=50, description="操作")
    is_active: Optional[bool] = Field(None, description="是否激活")


class PermissionResponse(PermissionBase, BaseModelSchema):
    """权限响应Schema"""
    is_active: bool = Field(..., description="是否激活")


class UserRoleAssign(BaseSchema):
    """用户角色分配Schema"""
    user_id: str = Field(..., description="用户ID")
    role_ids: List[str] = Field(..., description="角色ID列表")


class RolePermissionAssign(BaseSchema):
    """角色权限分配Schema"""
    role_id: str = Field(..., description="角色ID")
    permission_ids: List[str] = Field(..., description="权限ID列表")