using Netina.Domain.Entities.Warehouses;

namespace Netina.Repository.Handlers.Warehouses;

public class UpdateShippingCommandHandler(IRepositoryWrapper repositoryWrapper)
    : IRequestHandler<UpdateShippingCommand, bool>
{
    public async Task<bool> Handle(UpdateShippingCommand request, CancellationToken cancellationToken)
    {
        var ent = await repositoryWrapper.SetRepository<Shipping>().TableNoTracking
            .FirstOrDefaultAsync(s => s.Id == request.Id, cancellationToken);
        if (ent == null)
            throw new AppException("Shipping not found", ApiResultStatusCode.NotFound);

        var newEnt = Shipping.Create(request.Name, request.WarehouseName, request.IsExpressShipping, request.IsShipBySeller,
            request.IsOriginalWarehouse,request.DeliveryCost,request.WorkingDays);
        newEnt.Id = ent.Id;
        newEnt.CreatedAt = ent.CreatedAt;
        newEnt.CreatedBy = ent.CreatedBy;
        repositoryWrapper.SetRepository<Shipping>().Update(newEnt);
        await repositoryWrapper.SaveChangesAsync(cancellationToken);
        return true;
    }
}