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

namespace Indotalent.Features.Pipeline.LeadActivity.Cqrs;

public record DeleteLeadActivityByIdRequest(string Id);
public record DeleteLeadActivityByIdCommand(DeleteLeadActivityByIdRequest Data) : IRequest<bool>;

public class DeleteLeadActivityByIdHandler : IRequestHandler<DeleteLeadActivityByIdCommand, bool>
{
    private readonly AppDbContext _context;

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

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

        if (entity == null) return false;

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