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

namespace Indotalent.Features.Pipeline.Lead.Cqrs;

public record DeleteLeadByIdRequest(string Id);

public record DeleteLeadByIdCommand(DeleteLeadByIdRequest Data) : IRequest<bool>;

public class DeleteLeadByIdHandler : IRequestHandler<DeleteLeadByIdCommand, bool>
{
    private readonly AppDbContext _context;

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

    public async Task<bool> Handle(DeleteLeadByIdCommand request, CancellationToken cancellationToken)
    {
        var entity = await _context.Lead.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
        if (entity == null) return false;

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