package de.effigenix.application.masterdata.customer;

import de.effigenix.domain.masterdata.customer.*;
import de.effigenix.shared.common.Result;

import java.util.List;

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

public class ListCustomers {

    private final CustomerRepository customerRepository;

    public ListCustomers(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }

    public Result<CustomerError, List<Customer>> execute() {
        return switch (customerRepository.findAll()) {
            case Failure(var err) ->
                Result.failure(new CustomerError.RepositoryFailure(err.message()));
            case Success(var customers) ->
                Result.success(customers);
        };
    }

    public Result<CustomerError, List<Customer>> executeByType(CustomerType type) {
        return switch (customerRepository.findByType(type)) {
            case Failure(var err) ->
                Result.failure(new CustomerError.RepositoryFailure(err.message()));
            case Success(var customers) ->
                Result.success(customers);
        };
    }

    public Result<CustomerError, List<Customer>> executeByStatus(CustomerStatus status) {
        return switch (customerRepository.findByStatus(status)) {
            case Failure(var err) ->
                Result.failure(new CustomerError.RepositoryFailure(err.message()));
            case Success(var customers) ->
                Result.success(customers);
        };
    }
}
