"use client" import { getFoodById, getIngredients, getNutrients } from "@/services/data/foods" import { supabase } from "@/services/supabase" import type { Foods } from "@/types" import type { Ingredient, Nutrient } from "@/types/index" import { Feather, MaterialCommunityIcons, Ionicons } from "@expo/vector-icons" import { useQuery } from "@tanstack/react-query" import { Image } from "expo-image" import { router, useLocalSearchParams } from "expo-router" import { useRef } from "react" import { ScrollView, Text, TouchableOpacity, View, ActivityIndicator, Animated, Dimensions } from "react-native" import { LinearGradient } from "expo-linear-gradient" interface Step { id: string food_id: string title: string step_order: number description: string created_at: string } const { width } = Dimensions.get("window") export default function FoodDetailScreen() { const { id } = useLocalSearchParams() const scrollY = useRef(new Animated.Value(0)).current const foodId = typeof id === "string" ? id : "" const { data: foodData, isLoading, error, } = useQuery({ queryKey: ["food-detail", foodId], queryFn: async () => { const { data, error } = await getFoodById(foodId) if (error) throw error if (!data) throw new Error("Food not found") return data }, enabled: !!foodId, }) const { data: nutrients, isLoading: nutrientsLoading, error: nutrientsError, } = useQuery({ queryKey: ["food-nutrients", foodId], queryFn: async () => { const { data, error } = await getNutrients(foodId) if (error) throw error return data }, enabled: !!foodId && !!foodData, }) const { data: ingredients, error: ingredientsError, isLoading: ingredientsLoading, } = useQuery({ queryKey: ["food-ingredients", foodId], queryFn: async () => { const { data, error } = await getIngredients(foodId) if (error) throw error return data ?? [] }, enabled: !!foodId && !!foodData, }) const { data: steps, isLoading: stepsLoading, error: stepsError, } = useQuery({ queryKey: ["food-steps", foodId], queryFn: async () => { const { data, error } = await supabase .from("cooking_steps") .select( ` id, food_id, title, step_order, description, created_at `, ) .eq("food_id", foodId) .order("step_order", { ascending: true }) if (error) throw error return data ?? [] }, enabled: !!foodId && !!foodData, }) // Calculate header opacity based on scroll position const headerOpacity = scrollY.interpolate({ inputRange: [0, 100, 150], outputRange: [0, 0.5, 1], extrapolate: "clamp", }) // Calculate image scale based on scroll position const imageScale = scrollY.interpolate({ inputRange: [-100, 0, 100], outputRange: [1.2, 1, 0.8], extrapolate: "clamp", }) if (isLoading || stepsLoading || nutrientsLoading || ingredientsLoading) { return ( Loading recipe details... ) } if (error || !foodData || ingredientsError || stepsError || nutrientsError) { return ( Oops! Something went wrong We couldn't load the recipe details. Please try again later. router.push("/home")}> Go back to home ) } const startCookingSession = () => { router.push(`/cooking/${foodId}`) } // Recipe info cards data const recipeInfoCards = [ { id: "skill_level", title: "Skill Level", icon: , value: foodData.skill_level || "Easy", color: "#4CAF50", }, { id: "cooking_time", title: "Cooking Time", icon: , value: `${foodData.time_to_cook_minutes || 0} min`, color: "#bb0718", }, { id: "ingredients", title: "Ingredients", icon: , value: `${foodData.ingredient_count ?? ingredients?.length ?? 0}`, color: "#2196F3", }, { id: "calories", title: "Calories", icon: , value: `${foodData.calories || 0} kcal`, color: "#F44336", }, ] return ( {/* Animated header background */} {/* Header with back and share buttons */} router.back()} > {/* Food Image with gradient overlay */} {foodData.name} {foodData.skill_level || "Easy"} {foodData.time_to_cook_minutes || 0} min {/* Description */} {foodData.description && ( {foodData.description} )} {/* Recipe Info Cards - Horizontal Scrollable */} {recipeInfoCards.map((card) => ( {card.icon} {card.title} {card.value} ))} {/* Ingredients Section */} Ingredients {ingredients && ingredients.length > 0 ? ( {ingredients.map((ingredient, index) => ( {ingredient.emoji || "🍴"} {ingredient.name} ))} ) : ( No ingredients listed )} {/* Nutrition Section */} {nutrients && ( Nutrition Facts )} {/* Steps Preview */} Cooking Steps {steps && steps.length > 0 ? ( {steps.slice(0, 3).map((step, index) => ( {step.step_order ?? index + 1} {step.title && {step.title}} {step.description || "No description available"} ))} {steps.length > 3 && ( View all {steps.length} steps )} ) : ( No cooking steps available )} {/* Cook Button */} Let's Cook! ) } // Helper component for nutrition facts function NutrientCircle({ value, label, color, bgColor, }: { value: number; label: string; color: string; bgColor: string }) { return ( {value}g {label} ) }