package de.effigenix.application.masterdata;

import de.effigenix.domain.masterdata.*;
import de.effigenix.shared.common.Result;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

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

@Transactional(readOnly = true)
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);
        };
    }
}
