package com.eshore.gringotts.web.domain.entity;

import com.eshore.gringotts.core.Constants;
import com.eshore.gringotts.web.domain.base.entity.SimpleEntity;
import java.time.LocalDateTime;
import javax.persistence.Column;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.NamedAttributeNode;
import javax.persistence.NamedEntityGraph;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.DynamicUpdate;

/**
 * 用户
 *
 * @author lanyuanxiaoyao
 * @date 2024-11-14
 */
@Getter
@Setter
@ToString
@Entity
@DynamicUpdate
@Table(name = Constants.TABLE_PREFIX + "user")
@NamedEntityGraph(name = "user.list", attributeNodes = {
        @NamedAttributeNode(value = "createdUser"),
})
@NamedEntityGraph(name = "user.detail", attributeNodes = {
        @NamedAttributeNode(value = "createdUser"),
        @NamedAttributeNode(value = "modifiedUser"),
        @NamedAttributeNode(value = "checkedUser"),
})
public class User extends SimpleEntity {
    @Column(unique = true, nullable = false)
    private String username;
    @Column(nullable = false)
    private String password;
    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private Role role;
    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private State state = State.CHECKING;
    private LocalDateTime lastLoginTime;
    private LocalDateTime checkedTime;
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    @ToString.Exclude
    private User checkedUser;

    public static boolean isAdministrator(User user) {
        return Role.ADMINISTRATOR.equals(user.role);
    }

    public static boolean isNotAdministrator(User user) {
        return !isAdministrator(user);
    }

    public static boolean isChecker(User user) {
        return Role.CHECKER.equals(user.role);
    }

    public static boolean isNotChecker(User user) {
        return !isChecker(user);
    }

    public static boolean isProvider(User user) {
        return Role.PROVIDER.equals(user.role);
    }

    public static boolean isNotProvider(User user) {
        return !isProvider(user);
    }

    public static boolean isCustomer(User user) {
        return Role.CUSTOMER.equals(user.role);
    }

    public static boolean isNotCustomer(User user) {
        return !isCustomer(user);
    }

    public static boolean isUser(User user) {
        return isProvider(user) || isCustomer(user);
    }

    public enum State {
        /**
         * 审查中
         */
        CHECKING,
        /**
         * 正常
         */
        NORMAL,
        /**
         * 禁用
         */
        DISABLED,
    }

    public enum Role {
        /**
         * 数据提供方
         */
        PROVIDER,
        /**
         * 数据使用方
         */
        CUSTOMER,
        /**
         * 数据监管方
         */
        CHECKER,
        /**
         * 系统管理员
         */
        ADMINISTRATOR,
    }
}
