package app.mealsmadeeasy.api.recipe.converter;

import app.mealsmadeeasy.api.recipe.body.RecipeUpdateBody;
import app.mealsmadeeasy.api.recipe.spec.RecipeUpdateSpec;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Service;

@Service
public class RecipeUpdateBodyToSpecConverter {

    public RecipeUpdateSpec convert(RecipeUpdateBody body) {
        final var b = RecipeUpdateSpec.builder()
                .title(body.getTitle())
                .preparationTime(body.getPreparationTime())
                .cookingTime(body.getCookingTime())
                .totalTime(body.getTotalTime())
                .rawText(body.getRawText())
                .isPublic(body.getIsPublic());

        if (body.getIngredients() != null) {
            b.ingredients(body.getIngredients().stream()
                    .map(ingredient -> RecipeUpdateSpec.IngredientUpdateSpec.builder()
                            .amount(ingredient.getAmount())
                            .name(ingredient.getName())
                            .notes(ingredient.getNotes())
                            .build())
                    .toList()
            );
        }

        final @Nullable RecipeUpdateBody.MainImageUpdateBody mainImage = body.getMainImage();
        if (mainImage != null) {
            b.mainImage(
                    RecipeUpdateSpec.MainImageUpdateSpec.builder()
                            .username(mainImage.getUsername())
                            .filename(mainImage.getFilename())
                            .build()
            );
        }

        return b.build();
    }

}
