package de.effigenix.application.masterdata.supplier;

import de.effigenix.domain.masterdata.supplier.Supplier;
import de.effigenix.domain.masterdata.supplier.SupplierError;
import de.effigenix.domain.masterdata.supplier.SupplierId;
import de.effigenix.domain.masterdata.supplier.SupplierRepository;
import de.effigenix.shared.common.Result;

import static de.effigenix.shared.common.Result.*;

public class GetSupplier {

    private final SupplierRepository supplierRepository;

    public GetSupplier(SupplierRepository supplierRepository) {
        this.supplierRepository = supplierRepository;
    }

    public Result<SupplierError, Supplier> execute(SupplierId supplierId) {
        return switch (supplierRepository.findById(supplierId)) {
            case Failure(var err) ->
                Result.failure(new SupplierError.RepositoryFailure(err.message()));
            case Success(var opt) ->
                opt.map(Result::<SupplierError, Supplier>success)
                    .orElseGet(() -> Result.failure(new SupplierError.SupplierNotFound(supplierId)));
        };
    }
}
