package com.izouma.awesomeAdmin.security;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.izouma.awesomeAdmin.enums.AuthorityName;
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 static Authority get(AuthorityName name) {
        return new Authority(name.name(), name.getDescription());
    }

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

    @Column(length = 50)
    @Size(max = 50)
    @NotNull
    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 + '\'' +
                "}";
    }
}
