# _*_ coding : UTF-8 _*_
# @Time : 2025/01/19 01:35
# @UpdateTime : 2025/01/19 01:35
# @Author : sonder
# @File : login.py
# @Software : PyCharm
# @Comment : 本程序
from typing import Optional

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

from schemas.common import BaseResponse


class LoginParams(BaseModel):
    """
    登录请求模型
    """
    username: str = Field(default="", description="用户名")
    password: str = Field(default="", description="密码")
    loginDays: Optional[int] = Field(default=1, description="登录天数")
    code: Optional[str] = Field(default="", description="验证码")
    uuid: Optional[str] = Field(default="", description="验证码UUID")


class LoginResult(BaseModel):
    """
    登录响应模型
    """
    accessToken: str = Field(default="", description="访问令牌")
    refreshToken: str = Field(default="", description="刷新令牌")
    expiresTime: int = Field(default=0, description="令牌过期时间戳")


class LoginResponse(BaseResponse):
    """
    登录响应模型
    """
    data: LoginResult = Field(default=None, description="响应数据")


class UserInfo(BaseModel):
    """
    用户信息模型
    """
    id: str = Field(default="", description="用户ID")
    username: str = Field(default="", description="用户名")
    nickname: str = Field(default="", description="用户昵称")
    avatar: str = Field(default="", description="用户头像")
    gender: int = Field(default=0, description="用户性别")
    email: str = Field(default="", description="用户邮箱")
    phone: str = Field(default="", description="用户手机号")
    status: int = Field(default=0, description="用户状态")
    department_id: str = Field(default="", description="用户部门ID")
    department_name: str = Field(default="", description="用户部门名称")
    roles: list = Field(default=[], description="用户角色")
    permissions: list = Field(default=[], description="用户权限")
    create_time: str = Field(default="", description="创建时间")
    update_time: str = Field(default="", 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": "1",
                "username": "admin",
                "nickname": "管理员",
                "avatar": "https://www.example.com/avatar.jpg",
                "gender": 1,
                "email": "admin@example.com",
                "phone": "1234567890",
                "status": 1,
                "department_id": "1",
                "department_name": "技术部",
                "roles": ["admin"],
                "permissions": ["read", "write"],
                "create_time": "2022-01-01 00:00:00",
                "update_time": "2022-01-01 00:00:00"
            }
        }


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


class UpdateUserTokenResponse(BaseResponse):
    """
    更新用户令牌响应模型
    """
    data: LoginResult = Field(default=None, description="响应数据")


class GetCaptchaResult(BaseModel):
    """
    获取验证码结果模型
    """
    uuid: Optional[str] = Field(default=None, description="验证码UUID")
    captcha: Optional[str] = Field(default=None, description="验证码图片")
    captcha_enabled: Optional[bool] = Field(default=False, description="是否开启验证码")

    class Config:
        json_schema_extra = {
            "example": {
                "uuid": "1234567890",
                "captcha": "base64编码的图片",
                "captcha_enabled": True
            }
        }


class GetEmailCodeParams(BaseModel):
    """
    获取邮箱验证码请求模型
    """
    username: str = Field(default="", description="用户名")
    title: str = Field(default="注册", description="邮件类型")
    mail: str = Field(default="", description="邮箱地址")

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

    class Config:
        json_schema_extra = {
            "example": {
                "username": "admin",
                "title": "注册",
                "mail": "admin@example.com"
            }
        }


class ResetPasswordParams(BaseModel):
    """
    重置密码请求模型
    """
    username: str = Field(default="", description="用户名")
    mail: str = Field(default="", description="邮箱地址")
    code: str = Field(default="", description="验证码")
    password: str = Field(default="", description="新密码")

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

    @Size(field_name='code', min_length=0, max_length=4, message='验证码长度不能超过4个字符')
    def get_code(self):
        return self.code

    class Config:
        json_schema_extra = {
            "example": {
                "username": "admin",
                "mail": "admin@example.com",
                "code": "1234",
                "password": "123456"
            }
        }


class GetCaptchaResponse(BaseResponse):
    """
    获取验证码响应模型
    """
    data: GetCaptchaResult = Field(default=None, description="响应数据")
