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

namespace Indotalent.Features.Thirdparty.CustomerCategory.Cqrs;

public record DeleteCustomerCategoryByIdRequest(string Id);

public record DeleteCustomerCategoryByIdCommand(DeleteCustomerCategoryByIdRequest Data) : IRequest<bool>;

public class DeleteCustomerCategoryByIdHandler : IRequestHandler<DeleteCustomerCategoryByIdCommand, bool>
{
    private readonly AppDbContext _context;

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

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

        if (entity == null) return false;

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