package cn.reghao.tnb.auth.app.model.po;

import cn.reghao.jutil.jdk.web.db.BaseObject;
import cn.reghao.tnb.common.util.ConstantId;
import lombok.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Set;

/**
 * 用户账号
 * 实现的 UserDetails 接口提供了 Spring Security 需要的用户认证信息
 *
 * @author reghao
 * @date 2021-10-30 17:49:07
 */
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class UserAccount extends BaseObject<Integer> implements UserDetails {
    @NotNull
    private Long userId;
    @NotBlank
    @Size(min = 3, max = 10)
    private String username;
    @NotBlank
    @Size(min = 11, max = 11)
    private String mobile;
    @NotBlank
    @Size(min = 1, max = 255)
    private String encodedPassword;
    @NotBlank
    @Size(min = 64, max = 64)
    private String salt;
    @NotNull
    private LocalDateTime createAt;
    // Spring Security 使用的字段
    // 使用 UserRole 持久化到数据库
    private transient Set<UserAuthority> authorities;
    @NotNull
    private Boolean enabled;
    @NotNull
    private Boolean locked;

    @NotBlank
    @Size(min = 1, max = 20)
    private String screenName;
    @NotBlank
    @Size(min = 1, max = 64)
    private String avatarUrl;
    private String email;

    /**
     * 验证码登入创建的帐号
     *
     * @param
     * @return
     * @date 2025-10-19 17:10:646
     */
    public UserAccount(String mobile, long userId) {
        this.userId = userId;
        this.username = String.format("tnb_%s", userId);
        this.mobile = mobile;
        this.encodedPassword = "";
        this.salt = "";
        this.createAt = LocalDateTime.now();
        this.enabled = true;
        this.locked = false;
        this.screenName = this.username;
        this.avatarUrl = ConstantId.AVATAR_URL;
        this.email = String.format("tnb_%s@reghao.cn", userId);
        this.authorities = Set.of(new UserAuthority());
    }

    public UserAccount(String mobile, long userId, String encodedPassword, String salt, Set<UserAuthority> authorities) {
        this.userId = userId;
        this.username = String.format("tnb_%s", userId);
        this.mobile = mobile;
        this.encodedPassword = encodedPassword;
        this.salt = salt;
        this.createAt = LocalDateTime.now();
        this.enabled = true;
        this.locked = false;
        this.screenName = this.username;
        this.avatarUrl = ConstantId.AVATAR_URL;
        this.email = String.format("tnb_%s@reghao.cn", userId);
        this.authorities = authorities;
    }

    /**
     * 使用爬虫数据创建的帐号
     *
     * @param
     * @return
     * @date 2025-10-19 17:10:262
     */
    @Deprecated
    public UserAccount(String mobile, long userId, String username, String screenName, String avatarUrl) {
        this.userId = userId;
        this.username = username;
        this.mobile = mobile;
        this.encodedPassword = ConstantId.ENCODED_PASSWORD;
        this.salt = ConstantId.SALT;
        this.createAt = LocalDateTime.now();
        this.enabled = true;
        this.locked = false;
        this.screenName = screenName;
        this.avatarUrl = avatarUrl;
        this.email = "";
        this.authorities = Set.of(new UserAuthority());
    }

    public Long getUserId() {
        return userId;
    }

    public String getEncodedPassword() {
        return encodedPassword;
    }

    public String getSalt() {
        return salt;
    }

    public void setAuthorities(Set<UserAuthority> authorities) {
        this.authorities = authorities;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public String getPassword() {
        return this.encodedPassword;
    }

    /**
     * 账号是否未过期
     *
     * @param
     * @return
     * @date 2021-11-16 上午9:56
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 密码是否未过期
     *
     * @param
     * @return
     * @date 2021-11-16 上午9:56
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 账号是否可用
     *
     * @param
     * @return
     * @date 2021-11-16 上午9:56
     */
    @Override
    public boolean isEnabled() {
        return this.enabled;
    }

    /**
     * 账号是否锁定
     *
     * @param
     * @return
     * @date 2021-11-16 上午9:56
     */
    @Override
    public boolean isAccountNonLocked() {
        return !this.locked;
    }

    /**
     * 权限列表
     *
     * @param
     * @return
     * @date 2021-11-16 上午9:56
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }
}
