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

namespace Indotalent.Features.Thirdparty.CustomerGroup.Cqrs;

public record DeleteCustomerGroupByIdRequest(string Id);

public record DeleteCustomerGroupByIdCommand(DeleteCustomerGroupByIdRequest Data) : IRequest<bool>;

public class DeleteCustomerGroupByIdHandler : IRequestHandler<DeleteCustomerGroupByIdCommand, bool>
{
    private readonly AppDbContext _context;

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

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

        if (entity == null) return false;

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