package com.mzh.library.service;

import com.mzh.library.entity.Permission;
import com.mzh.library.entity.Role;

import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;

public class PermissionPolicy {
    public Set<Permission> permissionsFor(Role role) {
        if (role == null) {
            return Collections.emptySet();
        }

        switch (role) {
            case ADMINISTRATOR:
                return Collections.unmodifiableSet(EnumSet.allOf(Permission.class));
            case LIBRARIAN:
                return Collections.unmodifiableSet(EnumSet.of(
                    Permission.MANAGE_BOOKS,
                    Permission.MANAGE_READERS,
                    Permission.MANAGE_BORROWING,
                    Permission.VIEW_REPORTS,
                    Permission.VIEW_CATALOG
                ));
            case READER:
                return Collections.unmodifiableSet(EnumSet.of(
                    Permission.VIEW_CATALOG,
                    Permission.BORROW_BOOKS
                ));
            default:
                return Collections.emptySet();
        }
    }

    public boolean allows(Role role, Permission permission) {
        return permission != null && permissionsFor(role).contains(permission);
    }
}
