package de.effigenix.application.production;

import de.effigenix.domain.production.*;
import de.effigenix.shared.common.Result;
import de.effigenix.shared.security.ActorId;
import de.effigenix.shared.security.AuthorizationPort;

import java.time.LocalDate;
import java.util.List;

public class ListBatches {

    private final BatchRepository batchRepository;
    private final RecipeRepository recipeRepository;
    private final AuthorizationPort authorizationPort;

    public ListBatches(BatchRepository batchRepository, RecipeRepository recipeRepository,
                       AuthorizationPort authorizationPort) {
        this.batchRepository = batchRepository;
        this.recipeRepository = recipeRepository;
        this.authorizationPort = authorizationPort;
    }

    public Result<BatchError, List<Batch>> execute(ActorId performedBy) {
        if (!authorizationPort.can(performedBy, ProductionAction.BATCH_READ)) {
            return Result.failure(new BatchError.Unauthorized("Not authorized to read batches"));
        }

        switch (batchRepository.findAllSummary()) {
            case Result.Failure(var err) ->
                { return Result.failure(new BatchError.RepositoryFailure(err.message())); }
            case Result.Success(var batches) ->
                { return Result.success(batches); }
        }
    }

    public Result<BatchError, List<Batch>> executeByStatus(BatchStatus status, ActorId performedBy) {
        if (!authorizationPort.can(performedBy, ProductionAction.BATCH_READ)) {
            return Result.failure(new BatchError.Unauthorized("Not authorized to read batches"));
        }

        switch (batchRepository.findByStatusSummary(status)) {
            case Result.Failure(var err) ->
                { return Result.failure(new BatchError.RepositoryFailure(err.message())); }
            case Result.Success(var batches) ->
                { return Result.success(batches); }
        }
    }

    public Result<BatchError, List<Batch>> executeByProductionDate(LocalDate date, ActorId performedBy) {
        if (!authorizationPort.can(performedBy, ProductionAction.BATCH_READ)) {
            return Result.failure(new BatchError.Unauthorized("Not authorized to read batches"));
        }

        switch (batchRepository.findByProductionDateSummary(date)) {
            case Result.Failure(var err) ->
                { return Result.failure(new BatchError.RepositoryFailure(err.message())); }
            case Result.Success(var batches) ->
                { return Result.success(batches); }
        }
    }

    public Result<BatchError, List<Batch>> executeByArticleId(String articleId, ActorId performedBy) {
        if (!authorizationPort.can(performedBy, ProductionAction.BATCH_READ)) {
            return Result.failure(new BatchError.Unauthorized("Not authorized to read batches"));
        }

        switch (recipeRepository.findByArticleId(articleId)) {
            case Result.Failure(var err) ->
                { return Result.failure(new BatchError.RepositoryFailure(err.message())); }
            case Result.Success(var recipes) -> {
                if (recipes.isEmpty()) {
                    return Result.success(List.of());
                }
                List<RecipeId> recipeIds = recipes.stream().map(Recipe::id).toList();
                switch (batchRepository.findByRecipeIdsSummary(recipeIds)) {
                    case Result.Failure(var batchErr) ->
                        { return Result.failure(new BatchError.RepositoryFailure(batchErr.message())); }
                    case Result.Success(var batches) ->
                        { return Result.success(batches); }
                }
            }
        }
    }
}
