using MediatR;
using Microsoft.EntityFrameworkCore;
using SolutionErp.Application.Common.Exceptions;
using SolutionErp.Application.Common.Interfaces;

namespace SolutionErp.Application.Master.Suppliers.Commands.DeleteSupplier;

public record DeleteSupplierCommand(Guid Id) : IRequest;

public class DeleteSupplierCommandHandler : IRequestHandler<DeleteSupplierCommand>
{
    private readonly IApplicationDbContext _db;

    public DeleteSupplierCommandHandler(IApplicationDbContext db) => _db = db;

    public async Task Handle(DeleteSupplierCommand request, CancellationToken ct)
    {
        var entity = await _db.Suppliers.FirstOrDefaultAsync(x => x.Id == request.Id, ct)
            ?? throw new NotFoundException("Supplier", request.Id);
        _db.Suppliers.Remove(entity);  // AuditingInterceptor convert sang soft delete
        await _db.SaveChangesAsync(ct);
    }
}
