package com.izouma.dingdong.security;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Objects;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class Authority implements Serializable {
    public enum NAMES {

        ROLE_USER("普通用户"),

        ROLE_DEV("开发者"),

        ROLE_ADMIN("超级管理员"),

        ROLE_INDUSTRY("行业管理员"),

        ROLE_LOCALIZATION("本地化"),

        ROLE_FINANCE("财务"),

        ROLE_CUSTOMER("客服");

        public final String description;

        NAMES(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    public static Authority getInstance(NAMES name) {
        return new Authority(name.name(), name.getDescription());
    }

    @Id
    @Size(max = 50)
    @NotNull
    @Column(length = 50)
    private String name;

    private String description;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Authority authority = (Authority) o;

        return Objects.equals(name, authority.name);
    }

    @Override
    public int hashCode() {
        return name != null ? name.hashCode() : 0;
    }

    @Override
    public String toString() {
        return "Authority{" +
                "name='" + name + '\'' +
                "}";
    }
}
