# _*_ coding : UTF-8 _*_
# @Time : 2025/01/19 02:10
# @UpdateTime : 2025/01/19 02:10
# @Author : sonder
# @File : user.py
# @Software : PyCharm
# @Comment : 本程序
from datetime import datetime
from typing import Optional, List
from uuid import UUID

from pydantic import BaseModel, Field
from pydantic_validation_decorator import Xss, NotBlank, Size, Network

from schemas.common import BaseResponse, ListQueryResult


class UserBase(BaseModel):
    """
    用户表基础模型。
    """
    id: UUID = Field(..., description="主键")
    del_flag: int = Field(default=1, description="删除标志 1存在 0删除")
    create_by: str = Field(default="", description="创建者")
    create_time: str = Field(..., description="创建时间")
    update_by: str = Field(default="", description="更新者")
    update_time: str = Field(..., description="更新时间")
    username: str = Field(..., max_length=255, description="用户名")
    password: str = Field(..., max_length=255, description="密码")
    email: Optional[str] = Field(default=None, max_length=255, description="邮箱")
    phone: Optional[str] = Field(default=None, max_length=30, description="手机号")
    nickname: Optional[str] = Field(default=None, max_length=255, description="昵称")
    avatar: Optional[str] = Field(default=None, max_length=255, description="头像")
    gender: int = Field(default=0, description="性别（1男，0女）")
    status: int = Field(default=1, description="用户状态（1启用，0禁用）")
    department_id: Optional[str] = Field(default=None, max_length=36, description="所属部门")

    @Xss(field_name='username', message='用户账号不能包含脚本字符')
    @NotBlank(field_name='username', message='用户账号不能为空')
    @Size(field_name='username', min_length=0, max_length=30, message='用户账号长度不能超过30个字符')
    def get_user_name(self):
        return self.user_name

    @Xss(field_name='nickname', message='用户昵称不能包含脚本字符')
    @Size(field_name='nickname', min_length=0, max_length=30, message='用户昵称长度不能超过30个字符')
    def get_nick_name(self):
        return self.nick_name

    @Network(field_name='email', field_type='EmailStr', message='邮箱格式不正确')
    @Size(field_name='email', min_length=0, max_length=50, message='邮箱长度不能超过50个字符')
    def get_email(self):
        return self.email

    class Config:
        json_schema_extra = {
            "example": {
                "id": "550e8400-e29b-41d4-a716-446655440000",
                "del_flag": 1,
                "create_by": "admin",
                "create_time": "2025-01-19 02:10:00",
                "update_by": "admin",
                "update_time": "2025-01-19 02:10:00",
                "username": "admin",
                "password": "admin",
                "email": "admin@example.com",
                "phone": "13800138000",
                "nickname": "管理员",
                "gender": 1,
                "status": 1,
                "department_id": "550e8400-e29b-41d4-a716-446655440000"
            }
        }


class UserInfo(BaseModel):
    """
    用户表基础模型。
    """
    id: str = Field(..., description="主键")
    create_time: str = Field(..., description="创建时间")  # 使用蛇形命名
    update_time: str = Field(..., description="更新时间")  # 使用蛇形命名
    username: str = Field(..., max_length=255, description="用户名")
    email: Optional[str] = Field(default=None, max_length=255, description="邮箱")
    phone: Optional[str] = Field(default=None, max_length=30, description="手机号")
    nickname: Optional[str] = Field(default=None, max_length=255, description="昵称")
    avatar: Optional[str] = Field(default=None, max_length=255, description="头像")
    gender: int = Field(default=0, description="性别（1男，0女）")
    status: int = Field(default=1, description="用户状态（1启用，0禁用）")
    department_id: Optional[str] = Field(default=None, max_length=36, description="所属部门")

    class Config:
        json_schema_extra = {
            "example": {
                "id": "550e8400-e29b-41d4-a716-446655440000",
                "create_time": "2025-01-19 02:10:00",
                "update_time": "2025-01-19 02:10:00",
                "username": "admin",
                "email": "admin@example.com",
                "phone": "13800138000",
                "nickname": "管理员",
                "gender": 1,
                "status": 1,
                "department_id": "550e8400-e29b-41d4-a716-446655440000"
            }
        }


class GetUserInfoResponse(BaseResponse):
    """
    获取用户信息响应模型。
    """
    data: UserInfo = Field(default=None, description="响应数据")


class AddUserParams(BaseModel):
    """
    添加用户参数模型。
    """
    username: str = Field(..., max_length=255, description="用户名")
    password: str = Field(..., max_length=255, description="密码")
    email: Optional[str] = Field(default=None, max_length=255, description="邮箱")
    phone: Optional[str] = Field(default=None, max_length=30, description="手机号")
    nickname: Optional[str] = Field(default=None, max_length=255, description="昵称")
    avatar: Optional[str] = Field(default=None, max_length=255, description="头像")
    gender: int = Field(default=0, description="性别（1男，0女）")
    status: int = Field(default=1, description="用户状态（1启用，0禁用）")
    department_id: Optional[str] = Field(default=None, max_length=36, description="所属部门")

    @Xss(field_name='username', message='用户账号不能包含脚本字符')
    @NotBlank(field_name='username', message='用户账号不能为空')
    @Size(field_name='username', min_length=0, max_length=30, message='用户账号长度不能超过30个字符')
    def get_user_name(self):
        return self.user_name

    @Xss(field_name='nickname', message='用户昵称不能包含脚本字符')
    @Size(field_name='nickname', min_length=0, max_length=30, message='用户昵称长度不能超过30个字符')
    def get_nick_name(self):
        return self.nick_name

    @Network(field_name='email', field_type='EmailStr', message='邮箱格式不正确')
    @Size(field_name='email', min_length=0, max_length=50, message='邮箱长度不能超过50个字符')
    def get_email(self):
        return self.email

    class Config:
        json_schema_extra = {
            "example": {
                "username": "admin",
                "password": "admin",
                "email": "admin@example.com",
                "phone": "13800138000",
                "nickname": "管理员",
                "gender": 1,
                "status": 1,
                "department_id": "550e8400-e29b-41d4-a716-446655440000"
            }
        }


class RegisterUserParams(AddUserParams):
    """
    注册用户参数模型。
    """
    code: str = Field(..., max_length=10, description="验证码")

    class Config:
        json_schema_extra = {
            "example": {
                "username": "admin",
                "password": "admin",
                "email": "admin@example.com",
                "phone": "13800138000",
                "nickname": "管理员",
                "gender": 1,
                "status": 1,
                "department_id": "550e8400-e29b-41d4-a716-446655440000",
                "code": "123456"
            }
        }


class UpdateUserParams(BaseModel):
    """
    更新用户参数模型。
    """
    username: str = Field(..., max_length=255, description="用户名")
    email: Optional[str] = Field(default=None, max_length=255, description="邮箱")
    phone: Optional[str] = Field(default=None, max_length=30, description="手机号")
    nickname: Optional[str] = Field(default=None, max_length=255, description="昵称")
    avatar: Optional[str] = Field(default=None, max_length=255, description="头像")
    gender: int = Field(default=0, description="性别（1男，0女）")
    status: int = Field(default=1, description="用户状态（1启用，0禁用）")
    department_id: Optional[str] = Field(default=None, max_length=36, description="所属部门")

    @Xss(field_name='username', message='用户账号不能包含脚本字符')
    @NotBlank(field_name='username', message='用户账号不能为空')
    @Size(field_name='username', min_length=0, max_length=30, message='用户账号长度不能超过30个字符')
    def get_user_name(self):
        return self.user_name

    @Xss(field_name='nickname', message='用户昵称不能包含脚本字符')
    @Size(field_name='nickname', min_length=0, max_length=30, message='用户昵称长度不能超过30个字符')
    def get_nick_name(self):
        return self.nick_name

    @Network(field_name='email', field_type='EmailStr', message='邮箱格式不正确')
    @Size(field_name='email', min_length=0, max_length=50, message='邮箱长度不能超过50个字符')
    def get_email(self):
        return self.email

    class Config:
        json_schema_extra = {
            "example": {
                "username": "admin",
                "email": "admin@example.com",
                "phone": "13800138000",
                "nickname": "管理员",
                "gender": 1,
                "status": 1,
                "department_id": "550e8400-e29b-41d4-a716-446655440000"
            }
        }


class GetUserListResult(ListQueryResult):
    """
    获取用户列表结果模型。
    """
    result: List[UserInfo] = Field(default=[], description="部门列表")


class GetUserListResponse(BaseResponse):
    """
    获取用户列表响应模型。
    """
    data: GetUserListResult = Field(default=None, description="响应数据")


class AddUserRoleParams(BaseModel):
    """
    添加用户角色参数模型。
    """
    user_id: str = Field(..., max_length=36, description="用户ID")
    role_id: str = Field(default=[], max_length=36, description="角色ID")

    class Config:
        json_schema_extra = {
            "example": {
                "user_id": "550e8400-e29b-41d4-a716-446655440000",
                "role_ids": "550e8400-e29b-41d4-a716-446655440000"
            }
        }


class UpdateUserRoleParams(BaseModel):
    """
    更新用户角色参数模型。
    """
    user_id: str = Field(..., max_length=36, description="用户ID")
    role_ids: List[str] = Field(default=[], max_length=36, description="角色ID")

    class Config:
        json_schema_extra = {
            "example": {
                "user_id": "550e8400-e29b-41d4-a716-446655440000",
                "role_ids": ["550e8400-e29b-41d4-a716-446655440000"]
            }
        }


class UserRoleInfo(BaseModel):
    """
    用户角色信息模型。
    """
    id: str = Field(..., max_length=36, description="主键ID")
    user_id: str = Field(..., max_length=36, description="用户ID")
    user_name: str = Field(..., max_length=100, description="用户账号")
    role_name: str = Field(..., max_length=100, description="角色名称")
    role_code: str = Field(..., max_length=100, description="角色编码")
    role_id: str = Field(..., max_length=36, description="角色ID")
    create_time: datetime = Field(..., description="创建时间")
    update_time: datetime = Field(..., description="更新时间")

    @Xss(field_name='user_name', message='用户账号不能包含脚本字符')
    @NotBlank(field_name='user_name', message='用户账号不能为空')
    @Size(field_name='user_name', min_length=0, max_length=30, message='用户账号长度不能超过30个字符')
    def get_user_name(self):
        return self.user_name

    @Xss(field_name='role_name', message='角色名称不能包含脚本字符')
    @NotBlank(field_name='role_name', message='角色名称不能为空')
    @Size(field_name='role_name', min_length=0, max_length=30, message='角色名称长度不能超过30个字符')
    def get_role_name(self):
        return self.role_name

    class Config:
        json_schema_extra = {
            "example": {
                "id": "550e8400-e29b-41d4-a716-446655440000",
                "user_id": "550e8400-e29b-41d4-a716-446655440000",
                "user_name": "zhangsan",
                "role_name": "管理员",
                "role_code": "admin",
                "role_id": "550e8400-e29b-41d4-a716-446655440000",
                "create_time": "2023-10-01T12:00:00",
                "update_time": "2023-10-01T12:00:00"
            }
        }


class GetUserRoleInfoResponse(BaseResponse):
    """
    获取用户角色信息响应模型。
    """
    data: UserRoleInfo = Field(default=None, description="响应数据")


class GetUserRoleListResult(ListQueryResult):
    """
    获取用户角色列表结果模型。
    """
    result: List[UserRoleInfo] = Field(default=[], description="用户角色列表")


class GetUserRoleListResponse(BaseResponse):
    """
    获取用户角色列表响应模型。
    """
    data: GetUserRoleListResult = Field(default=None, description="响应数据")


class GetUserPermissionListResponse(BaseResponse):
    """
    获取用户权限列表响应模型。
    """
    data: List[str] = Field(default=[], description="响应数据")


class ResetPasswordParams(BaseModel):
    """
    重置密码参数模型。
    """
    password: str = Field(..., max_length=100, description="新密码")

    class Config:
        json_schema_extra = {
            "example": {
                "password": "123456"
            }
        }


class GetUserStatisticsResult(BaseModel):
    """
    用户统计信息模型。
    """
    today_count: int = Field(default=0, description="今日查询次数")
    this_month_count: int = Field(default=0, description="本月查询次数")
    last_month_count: int = Field(default=0, description="上月查询次数")
    before_14day_count_success: List[dict] = Field(default=[], description="最近14天成功查询次数")
    before_14day_count_fail: List[dict] = Field(default=[], description="最近14天失败查询次数")

    class Config:
        json_schema_extra = {
            "example": {
                "today_count": 0,
                "this_month_count": 0,
                "last_month_count": 0,
                "before_14day_count_success": [
                    {
                        "date": "2023-10-01",
                        "count": 0
                    }
                ],
                "before_14day_count_fail": [
                    {
                        "date": "2023-10-01",
                        "count": 0
                    }
                ]
            }
        }


class GetUserStatisticsResponse(BaseResponse):
    """
    获取用户统计信息响应模型。
    """
    data: GetUserStatisticsResult = Field(default=None, description="响应数据")
