using Ablera.Serdica.Authorization.Models;
using Microsoft.Extensions.Options;
using System.Linq;

namespace Ablera.Serdica.Authorization;
public class GroupsUtilities
{
    private readonly string[] KnownRoles;

    public GroupsUtilities(IOptions<RolesConfig> rolesOptions)
    {
        RolesConfig value = rolesOptions.Value;
        if (value == null)
        {
            KnownRoles = new string[0];
            return;
        }

        KnownRoles = (from x in value.UserRoles.Concat(value.OperationsRoles).Concat(value.UnderwriterRoles).Concat(value.OrganizationAdminRoles)
                .Concat(value.SuperUserRoles)
                      select x.ToUpper()).ToArray();
    }

    public string[] GetGroupsByRole(string[] roles)
    {
        var source = KnownRoles.Intersect(roles);
        if (!source.Any())
        {
            return new string[0];
        }

        return source.ToArray();
    }
}
