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

namespace Indotalent.Features.Thirdparty.VendorCategory.Cqrs;

public record DeleteVendorCategoryByIdRequest(string Id);

public record DeleteVendorCategoryByIdCommand(DeleteVendorCategoryByIdRequest Data) : IRequest<bool>;

public class DeleteVendorCategoryByIdHandler : IRequestHandler<DeleteVendorCategoryByIdCommand, bool>
{
    private readonly AppDbContext _context;

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

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

        if (entity == null) return false;

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