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

namespace Indotalent.Features.Thirdparty.Customer.Cqrs;

public record DeleteCustomerByIdRequest(string Id);

public record DeleteCustomerByIdCommand(DeleteCustomerByIdRequest Data) : IRequest<bool>;

public class DeleteCustomerByIdHandler : IRequestHandler<DeleteCustomerByIdCommand, bool>
{
    private readonly AppDbContext _context;

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

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

        if (entity == null) return false;

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