namespace OpenWarehouse.auth.api.Common.PrivilageManager;

public static class GlobalPrivilage
{
    public enum Privilege
    {
        GlobalAdmin,
        GlobalManageUser,
        GlobalCreateUser,
        GlobalDeleteUser,
        GlobalEditUser,
        GlobalManageRole,
        GlobalCreateRole,
        GlobalDeleteRole,
        GlobalEditRole,
        GlobalDeleteExpiredToken,
        GlobalDeactivateUserToken,
        GlobalUser
    }
    
    private static readonly Dictionary<Privilege, string> PrivilegeToStringMap = new()
    {
        { Privilege.GlobalAdmin, "Auth-Admin" },
        { Privilege.GlobalManageUser, "Auth-User-Manage" },
        { Privilege.GlobalCreateUser, "Auth-User-Create" },
        { Privilege.GlobalDeleteUser, "Auth-User-Delete" },
        { Privilege.GlobalEditUser, "Auth-User-Edit" },
        { Privilege.GlobalManageRole, "Auth-Role-Manage" },
        { Privilege.GlobalCreateRole, "Auth-Role-Create-" },
        { Privilege.GlobalDeleteRole, "Auth-Role-Delete" },
        { Privilege.GlobalEditRole, "Auth-Role-Edit" },
        { Privilege.GlobalDeleteExpiredToken, "Auth-Delete-Expired-Token" },
        { Privilege.GlobalDeactivateUserToken, "Auth-Deactivate-User-Token" },
        { Privilege.GlobalUser, "Auth-User" }
    };


    private static readonly Dictionary<Privilege, List<Privilege>> PrivilegeMap = new()
    {
        { Privilege.GlobalAdmin, new List<Privilege> { Privilege.GlobalAdmin } },
        { Privilege.GlobalManageUser, new List<Privilege> { Privilege.GlobalAdmin, Privilege.GlobalManageUser } },
        { Privilege.GlobalCreateUser, new List<Privilege> { Privilege.GlobalAdmin, Privilege.GlobalManageUser, Privilege.GlobalCreateUser } },
        { Privilege.GlobalDeleteUser, new List<Privilege> { Privilege.GlobalAdmin, Privilege.GlobalManageUser, Privilege.GlobalDeleteUser } },
        { Privilege.GlobalEditUser, new List<Privilege> { Privilege.GlobalAdmin, Privilege.GlobalManageUser, Privilege.GlobalEditUser } },
        { Privilege.GlobalManageRole, new List<Privilege> { Privilege.GlobalAdmin, Privilege.GlobalManageRole } },
        { Privilege.GlobalCreateRole, new List<Privilege> { Privilege.GlobalAdmin, Privilege.GlobalManageRole, Privilege.GlobalCreateRole } },
        { Privilege.GlobalDeleteRole, new List<Privilege> { Privilege.GlobalAdmin, Privilege.GlobalManageRole, Privilege.GlobalDeleteRole } },
        { Privilege.GlobalEditRole, new List<Privilege> { Privilege.GlobalAdmin, Privilege.GlobalManageRole, Privilege.GlobalEditRole } },
        { Privilege.GlobalDeactivateUserToken, new List<Privilege> { Privilege.GlobalAdmin, Privilege.GlobalDeactivateUserToken } },
        { Privilege.GlobalDeleteExpiredToken, new List<Privilege> { Privilege.GlobalAdmin, Privilege.GlobalDeleteExpiredToken } },
        { Privilege.GlobalUser , GetAllPrivilages()}
    };


    public static List<string> GetPrivilegesFor(this Privilege privilegeKey)
    {
        var privilegeStrings = new List<string>();
        
        if (PrivilegeMap.TryGetValue(privilegeKey, out var privileges))
        {
            foreach (var privilege in privileges)
            {
                if (PrivilegeToStringMap.TryGetValue(privilege, out var privilegeString))
                {
                    privilegeStrings.Add(privilegeString);
                }
            }
        }

        return privilegeStrings;
    }
    
    public static string? GetStringForPrivilege(this Privilege privilege)
    {

        bool isSuccess = PrivilegeToStringMap.TryGetValue(privilege, out var privilegeString);

        if (isSuccess)
        {
            return privilegeString;
        }


        return null;
    }
    
    public static List<string> GatherPrivilegeStrings()
    {
        var privilegeStrings = new List<string>();

        foreach (var privilege in GetAllPrivilages())
        {
            var privilegeString = privilege.GetStringForPrivilege();
            if (privilegeString != null)
            {
                privilegeStrings.Add(privilegeString);
            }
        }

        return privilegeStrings;
    }
    
    public static List<Privilege> GetAllPrivilages()
    {
        var allPrivileges = new List<Privilege>();
        foreach (var privilege in Enum.GetValues(typeof(Privilege)))
        {
            allPrivileges.Add((Privilege)privilege);
        }

        return allPrivileges;
    }
}