using System.Security.Claims;
using OpenIddict.Abstractions;
using Ablera.Serdica.Authority.Contracts;
using Microsoft.AspNetCore.Identity;
using System.Threading.Tasks;

using static OpenIddict.Abstractions.OpenIddictConstants;
using System.Linq;
using OpenIddict.Server.AspNetCore;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authentication.Cookies;

using static Ablera.Serdica.Authentication.Constants.ConstantsClass;

namespace Ablera.Serdica.Authority.Extensions;

public static class SerdicaPrincipalBuilder
{
    public static ClaimsPrincipal Build(IEnumerable<Claim> claims,  IEnumerable<string> scopes, string authenticationType)
    {
        var principal =
            new ClaimsPrincipal(
                new ClaimsIdentity(
                    claims,
                    authenticationType,
                    Claims.Name, 
                    Claims.Role));


        principal.SetResources(SerdicaAPIAudience);
        principal.SetScopes(scopes);
        principal.SetDestinations(c =>
            c.Type == Claims.Name ? new[] { Destinations.AccessToken,
                                            Destinations.IdentityToken }
                                   : new[] { Destinations.AccessToken });

        return principal;
    }
}
