package com.mzh.library.entity;

import java.util.Locale;

public enum Role {
    ADMINISTRATOR("administrator", "管理员"),
    LIBRARIAN("librarian", "馆员"),
    READER("reader", "读者");

    private final String code;
    private final String displayName;

    Role(String code, String displayName) {
        this.code = code;
        this.displayName = displayName;
    }

    public String getCode() {
        return code;
    }

    public String getDisplayName() {
        return displayName;
    }

    public static Role fromCode(String code) {
        if (code == null || code.trim().isEmpty()) {
            throw new IllegalArgumentException("Role code is required");
        }

        String normalized = code.trim().toLowerCase(Locale.ROOT);
        for (Role role : values()) {
            if (role.code.equals(normalized)) {
                return role;
            }
        }

        throw new IllegalArgumentException("Unsupported role code: " + code);
    }
}
