package de.effigenix.application.masterdata.article;

import de.effigenix.domain.masterdata.article.Article;
import de.effigenix.domain.masterdata.article.ArticleError;
import de.effigenix.domain.masterdata.article.ArticleRepository;
import de.effigenix.domain.masterdata.article.ArticleStatus;
import de.effigenix.domain.masterdata.productcategory.ProductCategoryId;
import de.effigenix.shared.common.Result;

import java.util.List;

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

public class ListArticles {

    private final ArticleRepository articleRepository;

    public ListArticles(ArticleRepository articleRepository) {
        this.articleRepository = articleRepository;
    }

    public Result<ArticleError, List<Article>> execute() {
        return switch (articleRepository.findAll()) {
            case Failure(var err) ->
                Result.failure(new ArticleError.RepositoryFailure(err.message()));
            case Success(var articles) ->
                Result.success(articles);
        };
    }

    public Result<ArticleError, List<Article>> executeByCategory(ProductCategoryId categoryId) {
        return switch (articleRepository.findByCategory(categoryId)) {
            case Failure(var err) ->
                Result.failure(new ArticleError.RepositoryFailure(err.message()));
            case Success(var articles) ->
                Result.success(articles);
        };
    }

    public Result<ArticleError, List<Article>> executeByStatus(ArticleStatus status) {
        return switch (articleRepository.findByStatus(status)) {
            case Failure(var err) ->
                Result.failure(new ArticleError.RepositoryFailure(err.message()));
            case Success(var articles) ->
                Result.success(articles);
        };
    }
}
