using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using NexusReader.Domain.Entities;

namespace NexusReader.Web.Services;

public class CustomUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<NexusUser, IdentityRole>
{
    public CustomUserClaimsPrincipalFactory(
        UserManager<NexusUser> userManager,
        RoleManager<IdentityRole> roleManager,
        IOptions<IdentityOptions> optionsAccessor)
        : base(userManager, roleManager, optionsAccessor)
    {
    }

    protected override async Task<ClaimsIdentity> GenerateClaimsAsync(NexusUser user)
    {
        var identity = await base.GenerateClaimsAsync(user);
        if (!string.IsNullOrEmpty(user.TenantId))
        {
            identity.AddClaim(new Claim("TenantId", user.TenantId));
        }
        return identity;
    }
}
