package dev.fyloz.trial.colorrecipesexplorer.service

import dev.fyloz.trial.colorrecipesexplorer.model.Recipe
import dev.fyloz.trial.colorrecipesexplorer.model.RecipeStep
import dev.fyloz.trial.colorrecipesexplorer.repository.RecipeStepRepository
import org.springframework.stereotype.Service
import javax.transaction.Transactional

interface RecipeStepService : ModelService<RecipeStep, RecipeStepRepository> {
    /** Creates a step for the given [recipe] with the given [message]. */
    fun createForRecipe(recipe: Recipe, message: String): RecipeStep

    /** Creates several steps for the given [recipe] with the given [messages]. */
    fun createAllForRecipe(recipe: Recipe, messages: Collection<String>): Collection<RecipeStep>
}

@Service
class RecipeStepServiceImpl(recipeStepRepository: RecipeStepRepository) :
    AbstractModelService<RecipeStep, RecipeStepRepository>(recipeStepRepository),
    RecipeStepService {
    override fun createForRecipe(recipe: Recipe, message: String): RecipeStep =
        RecipeStep(recipe, message)

    @Transactional
    override fun createAllForRecipe(recipe: Recipe, messages: Collection<String>): Collection<RecipeStep> =
        messages.map { createForRecipe(recipe, it) }
}
