using Knot.Shared.Kernel;
using Knot.Contracts.Auth.Application.Abstractions;
using Knot.Contracts.Auth.Domain;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Knot.Modules.Auth.Application.Users;

internal sealed class GetUsersExistenceQueryHandler : IQueryHandler<GetUsersExistenceQuery, List<Guid>>
{
    private readonly IAuthDbContext _context;

    public GetUsersExistenceQueryHandler(IAuthDbContext context)
    {
        _context = context;
    }

    public async Task<Result<List<Guid>>> Handle(GetUsersExistenceQuery request, CancellationToken cancellationToken)
    {
        var existingIds = await _context.Set<Knot.Modules.Auth.Domain.User>()
            .Where(u => request.UserIds.Contains(u.Id))
            .Select(u => u.Id)
            .ToListAsync(cancellationToken);

        return Result.Success(existingIds);
    }
}
