using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Indotalent.Features.Pipeline.LeadContact.Cqrs;

public record DeleteLeadContactByIdRequest(string Id);

public record DeleteLeadContactByIdCommand(DeleteLeadContactByIdRequest Data) : IRequest<bool>;

public class DeleteLeadContactByIdHandler : IRequestHandler<DeleteLeadContactByIdCommand, bool>
{
    private readonly AppDbContext _context;

    public DeleteLeadContactByIdHandler(AppDbContext context) => _context = context;

    public async Task<bool> Handle(DeleteLeadContactByIdCommand request, CancellationToken cancellationToken)
    {
        var entity = await _context.LeadContact
            .FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);

        if (entity == null) return false;

        _context.LeadContact.Remove(entity);
        return await _context.SaveChangesAsync(cancellationToken) > 0;
    }
}