using API.Models;
using Microsoft.AspNetCore.Identity;

namespace API.Contracts.Auth
{
    public static class CurrentUserResponseExtensions
    {
        public static async Task<CurrentUserResponse> ToCurrentUserResponseAsync(
            this AppUser user,
            UserManager<AppUser> userManager)
        {
            ArgumentNullException.ThrowIfNull(user);
            ArgumentNullException.ThrowIfNull(userManager);

            var roles = await userManager.GetRolesAsync(user);

            return new CurrentUserResponse
            {
                Id = user.Id,
                UserName = user.UserName,
                Roles = roles.OrderBy(role => role).ToList(),
                IsActive = user.IsActive,
                MustChangePassword = user.MustChangePassword,
            };
        }
    }
}
