using Knot.Shared.Kernel;

namespace Knot.Modules.Auth.Infrastructure.Services;

public sealed class UserDisplayNameProvider : IUserDisplayNameProvider
{
    private readonly IUserRepository _userRepository;

    public UserDisplayNameProvider(IUserRepository userRepository)
    {
        _userRepository = userRepository;
    }

    public async Task<string> GetDisplayNameAsync(Guid userId, CancellationToken ct = default)
    {
        var user = await _userRepository.GetByIdAsync(userId, ct);
        return user?.DisplayName ?? "User";
    }

    public async Task<UserInfo?> GetUserInfoAsync(Guid userId, CancellationToken ct = default)
    {
        var user = await _userRepository.GetByIdAsync(userId, ct);
        if (user == null)
        {
            return null;
        }

        return new UserInfo(user.Id, user.Username, user.DisplayName, user.Avatar);
    }

    public async Task<IReadOnlyDictionary<Guid, UserInfo>> GetUsersInfoAsync(IEnumerable<Guid> userIds, CancellationToken ct = default)
    {
        var users = await _userRepository.GetByIdsAsync(userIds, ct);
        var result = new System.Collections.Generic.Dictionary<Guid, UserInfo>();
        foreach (var user in users)
        {
            result[user.Id] = new UserInfo(user.Id, user.Username, user.DisplayName, user.Avatar);
        }
        return result;
    }
}

