package app.mealsmadeeasy.api.recipe.spec;

import app.mealsmadeeasy.api.image.Image;
import app.mealsmadeeasy.api.recipe.Recipe;
import lombok.Builder;
import lombok.Value;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.List;

// For now, we cannot change slug after creation.
// In the future, we may be able to have redirects from
// old slugs to new slugs.
@Value
@Builder
public class RecipeUpdateSpec {

    @Value
    @Builder
    public static class MainImageUpdateSpec {
        String username;
        String filename;
    }

    @Builder
    public record IngredientUpdateSpec(@Nullable String amount, String name, @Nullable String notes) {}

    // For testing convenience only.
    @ApiStatus.Internal
    public static RecipeUpdateSpec.RecipeUpdateSpecBuilder fromRecipeToBuilder(Recipe recipe) {
        final var b = RecipeUpdateSpec.builder()
                .title(recipe.getTitle())
                .preparationTime(recipe.getPreparationTime())
                .cookingTime(recipe.getCookingTime())
                .totalTime(recipe.getTotalTime())
                .rawText(recipe.getRawText())
                .isPublic(recipe.getIsPublic());
        final @Nullable Image mainImage = recipe.getMainImage();
        if (recipe.getMainImage() != null) {
            b.mainImage(MainImageUpdateSpec.builder()
                    .username(mainImage.getOwner().getUsername())
                    .filename(mainImage.getUserFilename())
                    .build()
            );
        }
        return b;
    }

    String title;
    @Nullable Integer preparationTime;
    @Nullable Integer cookingTime;
    @Nullable Integer totalTime;
    @Nullable List<IngredientUpdateSpec> ingredients;
    String rawText;
    Boolean isPublic;
    @Nullable MainImageUpdateSpec mainImage;

}
