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.SupplierRepository;
import de.effigenix.domain.masterdata.supplier.SupplierStatus;
import de.effigenix.shared.common.Result;

import java.util.List;

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

public class ListSuppliers {

    private final SupplierRepository supplierRepository;

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

    public Result<SupplierError, List<Supplier>> execute() {
        return switch (supplierRepository.findAll()) {
            case Failure(var err) ->
                Result.failure(new SupplierError.RepositoryFailure(err.message()));
            case Success(var suppliers) ->
                Result.success(suppliers);
        };
    }

    public Result<SupplierError, List<Supplier>> executeByStatus(SupplierStatus status) {
        return switch (supplierRepository.findByStatus(status)) {
            case Failure(var err) ->
                Result.failure(new SupplierError.RepositoryFailure(err.message()));
            case Success(var suppliers) ->
                Result.success(suppliers);
        };
    }
}
