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

namespace Indotalent.Features.Setting.Tax.Cqrs;

public record DeleteTaxByIdRequest(string Id);

public class DeleteTaxByIdHandler : IRequestHandler<DeleteTaxByIdCommand, bool>
{
    private readonly AppDbContext _context;

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

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

        if (entity == null) return false;

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

public record DeleteTaxByIdCommand(DeleteTaxByIdRequest Data) : IRequest<bool>;