namespace Backend.Security;

public static class AdminRoles
{
    public const string Viewer = "viewer";
    public const string Member = "member";
    public const string Reviewer = "reviewer";
    public const string OrganizationTeam = "organization_team";
    public const string ContentAdmin = "content_admin";
    public const string Admin = "admin";
    public const string Owner = "owner";
    public const string Creator = "creator";

    public static string Normalize(string? role)
    {
        var normalizedRole = (role ?? string.Empty).Trim().ToLowerInvariant().Replace('-', '_');

        return normalizedRole switch
        {
            Owner => Owner,
            Creator => Creator,
            Admin => Admin,
            ContentAdmin => ContentAdmin,
            "creator_role" => Creator,
            "creatorrolle" => Creator,
            "admins" => Admin,
            "mitglied" => Member,
            "member" => Member,
            "reviewer" => Reviewer,
            "organisation_team" => OrganizationTeam,
            "organisationteam" => OrganizationTeam,
            "organization_team" => OrganizationTeam,
            "organizationteam" => OrganizationTeam,
            _ => Viewer,
        };
    }

    public static bool IsKnownRole(string? role)
    {
        var normalizedRole = (role ?? string.Empty).Trim().ToLowerInvariant().Replace('-', '_');
        return Normalize(normalizedRole) is Viewer or Member or Reviewer or OrganizationTeam or ContentAdmin or Admin or Owner or Creator;
    }

    public static bool CanAccessAdmin(string? role) =>
        Normalize(role) is Member or Reviewer or OrganizationTeam or ContentAdmin or Admin or Owner or Creator;

    public static bool CanManageContent(string? role) =>
        Normalize(role) is ContentAdmin or Admin or Owner or Creator;

    public static bool CanManageAdminWorkspace(string? role) =>
        Normalize(role) is Admin or Owner or Creator;

    public static bool CanManageOperationalSettings(string? role) =>
        Normalize(role) is Owner or Creator;

    public static bool CanManageTeam(string? role) =>
        Normalize(role) is Admin or Owner or Creator;

    public static bool CanResetTeamPasswords(string? role) =>
        Normalize(role) is Owner or Creator;

    public static bool CanDeleteTeamMembers(string? role) =>
        Normalize(role) is Owner or Creator;

    public static bool IsPrivilegedFullControlRole(string? role) =>
        Normalize(role) is Owner or Creator;
}
