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

using Knot.Contracts.Relations.Application.Contacts;

namespace Knot.Modules.Relations.Application.Contacts;

internal sealed class GetBlockedUserIdsQueryHandler : IQueryHandler<GetBlockedUserIdsQuery, List<Guid>>
{
    private readonly IContactsDbContext _context;

    public GetBlockedUserIdsQueryHandler(IContactsDbContext context)
    {
        _context = context;
    }

    public async Task<Result<List<Guid>>> Handle(GetBlockedUserIdsQuery request, CancellationToken cancellationToken)
    {
        var blockedIds = await _context.Contacts
            .Where(c => (c.UserId == request.UserId || c.ContactId == request.UserId) && c.Status == ContactStatus.Blocked)
            .Select(c => c.UserId == request.UserId ? c.ContactId : c.UserId)
            .ToListAsync(cancellationToken);

        return Result.Success(blockedIds);
    }
}
