package app.mealsmadeeasy.api.recipe.star;

import app.mealsmadeeasy.api.recipe.Recipe;
import app.mealsmadeeasy.api.user.User;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Optional;

public interface RecipeStarRepository extends JpaRepository<RecipeStar, Long> {

    Optional<RecipeStar> findByRecipeIdAndOwnerId(Integer recipeId, Integer ownerId);
    Optional<RecipeStar> findByRecipeAndOwner(Recipe recipe, User owner);

    @Query("SELECT count(rs) > 0 FROM RecipeStar rs, Recipe r WHERE r.owner.username = ?1 AND r.slug = ?2 AND r.id = rs.recipe.id AND rs.owner.id = ?3")
    boolean isStarer(String ownerUsername, String slug, Integer viewerId);

    @Transactional
    void deleteByRecipeIdAndOwnerId(Integer recipeId, Integer ownerId);

}
