package app.mealsmadeeasy.api.recipe.converter;

import app.mealsmadeeasy.api.image.converter.ImageToViewConverter;
import app.mealsmadeeasy.api.recipe.Recipe;
import app.mealsmadeeasy.api.recipe.RecipeService;
import app.mealsmadeeasy.api.recipe.view.RecipeInfoView;
import app.mealsmadeeasy.api.user.User;
import app.mealsmadeeasy.api.user.view.UserInfoView;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class RecipeToInfoViewConverter {

    private final ImageToViewConverter imageToViewConverter;
    private final RecipeService recipeService;

    public RecipeInfoView convert(@NotNull Recipe recipe, @Nullable User viewer) {
        return RecipeInfoView.builder()
                .id(recipe.getId())
                .created(recipe.getCreated())
                .modified(recipe.getModified())
                .slug(recipe.getSlug())
                .title(recipe.getTitle())
                .preparationTime(recipe.getPreparationTime())
                .cookingTime(recipe.getCookingTime())
                .totalTime(recipe.getTotalTime())
                .owner(UserInfoView.from(recipe.getOwner()))
                .isPublic(recipe.getIsPublic())
                .starCount(this.recipeService.getStarCount(recipe))
                .mainImage(this.imageToViewConverter.convert(recipe.getMainImage(), viewer, false))
                .build();
    }

}
