package com.iqser.red.service.persistence.management.v1.processor.roles;

import static com.iqser.red.service.persistence.management.v1.processor.roles.ActionRoles.*;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

import com.google.common.collect.Sets;

public final class ApplicationRoles {

    public static final String KNECON_ADMIN_ROLE = "KNECON_ADMIN";
    public static final String KNECON_SUPPORT_ROLE = "KNECON_SUPPORT";
    public static final String RED_USER_ROLE = "RED_USER";
    public static final String RED_MANAGER_ROLE = "RED_MANAGER";
    public static final String RED_ADMIN_ROLE = "RED_ADMIN";
    public static final String RED_USER_ADMIN_ROLE = "RED_USER_ADMIN";

    public static final Set<String> VALID_MEMBER_ROLES = Set.of(ApplicationRoles.RED_USER_ROLE, ApplicationRoles.RED_MANAGER_ROLE);

    public static final Set<String> UNMAPPED_ACTION_ROLES = Sets.newHashSet(UNARCHIVE_DOSSIER, UPDATE_LICENSE, GET_RSS, USE_SUPPORT_CONTROLLER);

    public static final Set<String> KNECON_SUPPORT_ACTION_ROLES = Sets.newHashSet(READ_LICENSE,
                                                                                  UPDATE_LICENSE,
                                                                                  GET_TENANTS,
                                                                                  CREATE_TENANT,
                                                                                  READ_USERS, READ_ALL_USERS, READ_USER_STATS,
                                                                                  WRITE_USERS,
                                                                                  READ_SMTP_CONFIGURATION,
                                                                                  WRITE_SMTP_CONFIGURATION,
                                                                                  UNARCHIVE_DOSSIER,
                                                                                  USE_SUPPORT_CONTROLLER);

    public static final Set<String> KNECON_ADMIN_ACTION_ROLES = Sets.union(KNECON_SUPPORT_ACTION_ROLES, Sets.newHashSet(IMPORT_FILES));

    public static final Set<String> RED_USER_ACTION_ROLES = Sets.newHashSet(ADD_COMMENT,
                                                                            READ_LICENSE,
                                                                            READ_APP_CONFIG,
                                                                            READ_DOSSIER_STATUS,
                                                                            ADD_DOSSIER_DICTIONARY_ENTRY,
                                                                            DO_MANUAL_REDACTION,
                                                                            ADD_UPDATE_DOSSIER_DICTIONARY_TYPE,
                                                                            DELETE_COMMENT,
                                                                            DELETE_DOSSIER_DICTIONARY_ENTRY,
                                                                            DELETE_DOSSIER_DICTIONARY_TYPE,
                                                                            DELETE_FILE,
                                                                            DELETE_MANUAL_REDACTION,
                                                                            DOWNLOAD_ANNOTATED_FILE,
                                                                            DOWNLOAD_ORIGINAL_FILE,
                                                                            DOWNLOAD_REDACTED_FILE,
                                                                            DOWNLOAD_REDACTION_PREVIEW_FILE,
                                                                            DOWNLOAD_REPORT_TEMPLATE,
                                                                            EXCLUDE_INCLUDE_FILE,
                                                                            EXCLUDE_INCLUDE_PAGES,
                                                                            GET_REPORT_TEMPLATES,
                                                                            GET_SIMILAR_IMAGES,
                                                                            MANAGE_USER_PREFERENCES,
                                                                            MANAGE_VIEWED_PAGES,
                                                                            PROCESS_DOWNLOAD,
                                                                            PROCESS_MANUAL_REDACTION_REQUEST,
                                                                            READ_COLORS,
                                                                            READ_DICTIONARY_TYPES,
                                                                            READ_DIGITAL_SIGNATURE, READ_DOSSIER, READ_USER_STATS,
                                                                            READ_DOSSIER_ATTRIBUTES,
                                                                            READ_DOSSIER_ATTRIBUTES_CONFIG,
                                                                            READ_DOSSIER_TEMPLATES,
                                                                            READ_DOWNLOAD_STATUS,
                                                                            READ_FILE_ATTRIBUTES_CONFIG,
                                                                            READ_FILE_STATUS,
                                                                            READ_GENERAL_CONFIGURATION,
                                                                            READ_LEGAL_BASIS,
                                                                            READ_MANUAL_REDACTIONS,
                                                                            READ_NOTIFICATIONS,
                                                                            READ_REDACTION_LOG,
                                                                            READ_RULES,
                                                                            READ_DATA_FORMATS,
                                                                            READ_USERS,
                                                                            READ_VERSIONS,
                                                                            READ_WATERMARK,
                                                                            REANALYZE_DOSSIER,
                                                                            REANALYZE_FILE,
                                                                            REQUEST_MANUAL_REDACTION,
                                                                            ROTATE_PAGE,
                                                                            SEARCH,
                                                                            SEARCH_AUDIT_LOG,
                                                                            SET_REVIEWER,
                                                                            SET_STATUS_APPROVED,
                                                                            SET_STATUS_UNDER_APPROVAL,
                                                                            UPDATE_MY_PROFILE,
                                                                            UPDATE_NOTIFICATIONS,
                                                                            UPLOAD_FILE,
                                                                            WRITE_FILE_ATTRIBUTES,
                                                                            PROCESS_TEXT_HIGHLIGHTS,
                                                                            GET_HIGHLIGHTS,
                                                                            CONVERT_HIGHLIGHTS,
                                                                            DELETE_HIGHLIGHTS,
                                                                            DELETE_IMPORTED_REDACTIONS);

    public static final Set<String> RED_ADMIN_ACTION_ROLES = Sets.newHashSet(ADD_DICTIONARY_ENTRY,
                                                                             ADD_UPDATE_DICTIONARY_TYPE,
                                                                             WRITE_DOSSIER_STATUS,
                                                                             READ_DOSSIER_STATUS,
                                                                             DELETE_DICTIONARY_ENTRY,
                                                                             DELETE_DICTIONARY_TYPE,
                                                                             DELETE_REPORT_TEMPLATE,
                                                                             DOWNLOAD_REPORT_TEMPLATE,
                                                                             GET_REPORT_TEMPLATES,
                                                                             MANAGE_USER_PREFERENCES,
                                                                             GET_SIMILAR_IMAGES,
                                                                             READ_COLORS,
                                                                             READ_DICTIONARY_TYPES,
                                                                             READ_DIGITAL_SIGNATURE,
                                                                             READ_DOSSIER_ATTRIBUTES,
                                                                             READ_DOSSIER_ATTRIBUTES_CONFIG,
                                                                             READ_DOSSIER_TEMPLATES,
                                                                             READ_FILE_ATTRIBUTES_CONFIG,
                                                                             READ_LEGAL_BASIS,
                                                                             READ_LICENSE_REPORT, READ_NOTIFICATIONS, READ_USER_STATS,
                                                                             READ_RULES,
                                                                             READ_DATA_FORMATS,
                                                                             READ_SMTP_CONFIGURATION,
                                                                             READ_VERSIONS,
                                                                             READ_WATERMARK,
                                                                             REINDEX,
                                                                             SEARCH_AUDIT_LOG,
                                                                             UPDATE_NOTIFICATIONS,
                                                                             UPLOAD_REPORT_TEMPLATE,
                                                                             WRITE_COLORS,
                                                                             WRITE_DIGITAL_SIGNATURE,
                                                                             WRITE_DOSSIER_ATTRIBUTES_CONFIG,
                                                                             WRITE_DOSSIER_TEMPLATES,
                                                                             WRITE_FILE_ATTRIBUTES_CONFIG,
                                                                             WRITE_GENERAL_CONFIGURATION,
                                                                             WRITE_LEGAL_BASIS,
                                                                             WRITE_RULES,
                                                                             WRITE_DATA_FORMATS,
                                                                             WRITE_SMTP_CONFIGURATION,
                                                                             WRITE_WATERMARK,
                                                                             WRITE_APP_CONFIG,
                                                                             MANAGE_ACL_PERMISSIONS,
                                                                             CREATE_TENANT,
                                                                             GET_TENANTS,
                                                                             DEPLOYMENT_INFO);

    public static final Set<String> RED_MANAGER_ACTION_ROLES = Sets.newHashSet(ADD_UPDATE_DOSSIER, ARCHIVE_DOSSIER, DELETE_DOSSIER, READ_USER_STATS, WRITE_DOSSIER_ATTRIBUTES);

    public static final Set<String> RED_USER_ADMIN_ACTION_ROLES = Sets.newHashSet(MANAGE_USER_PREFERENCES,
                                                                                  READ_ALL_USERS,
                                                                                  READ_APP_CONFIG,
                                                                                  READ_GENERAL_CONFIGURATION,
                                                                                  READ_GENERAL_CONFIGURATION,
                                                                                  GET_SIMILAR_IMAGES, READ_NOTIFICATIONS, READ_USER_STATS,
                                                                                  READ_USERS, READ_USER_STATS,
                                                                                  UPDATE_MY_PROFILE,
                                                                                  UPDATE_NOTIFICATIONS,
                                                                                  WRITE_USERS,
                                                                                  READ_LICENSE);

    public static final Map<String, Set<String>> ROLE_DATA = Map.of(KNECON_ADMIN_ROLE,
                                                                    KNECON_ADMIN_ACTION_ROLES,
                                                                    KNECON_SUPPORT_ROLE,
                                                                    KNECON_SUPPORT_ACTION_ROLES,
                                                                    RED_USER_ROLE,
                                                                    RED_USER_ACTION_ROLES,
                                                                    RED_MANAGER_ROLE,
                                                                    RED_MANAGER_ACTION_ROLES,
                                                                    RED_ADMIN_ROLE,
                                                                    RED_ADMIN_ACTION_ROLES,
                                                                    RED_USER_ADMIN_ROLE,
                                                                    RED_USER_ADMIN_ACTION_ROLES);


    private ApplicationRoles() {}


    public static void validateRoles(Collection<String> roles) {

        for (String role : roles) {
            if (!ROLE_DATA.containsKey(role)) {
                throw new IllegalArgumentException("Invalid Role: " + role);
            }
        }
    }

}
