import 'recipe.dart'; /// A user's bookmarked recipe. Display fields are joined from dishes + recipes. class SavedRecipe { final String id; final String recipeId; final String title; final String? description; /// Mapped from cuisine_slug in the API response. final String? cuisine; final String? difficulty; final int? prepTimeMin; final int? cookTimeMin; final int? servings; final String? imageUrl; final List ingredients; final List steps; final List tags; final DateTime savedAt; // Individual nutrition columns (nullable). final double? caloriesPerServing; final double? proteinPerServing; final double? fatPerServing; final double? carbsPerServing; const SavedRecipe({ required this.id, required this.recipeId, required this.title, this.description, this.cuisine, this.difficulty, this.prepTimeMin, this.cookTimeMin, this.servings, this.imageUrl, this.ingredients = const [], this.steps = const [], this.tags = const [], required this.savedAt, this.caloriesPerServing, this.proteinPerServing, this.fatPerServing, this.carbsPerServing, }); /// Builds a [NutritionInfo] from the individual per-serving columns, /// or null when no calorie data is available. NutritionInfo? get nutrition { if (caloriesPerServing == null) return null; return NutritionInfo( calories: caloriesPerServing!, proteinG: proteinPerServing ?? 0, fatG: fatPerServing ?? 0, carbsG: carbsPerServing ?? 0, approximate: false, ); } factory SavedRecipe.fromJson(Map json) { return SavedRecipe( id: json['id'] as String? ?? '', recipeId: json['recipe_id'] as String? ?? '', title: json['title'] as String? ?? '', description: json['description'] as String?, cuisine: json['cuisine_slug'] as String?, difficulty: json['difficulty'] as String?, prepTimeMin: (json['prep_time_min'] as num?)?.toInt(), cookTimeMin: (json['cook_time_min'] as num?)?.toInt(), servings: (json['servings'] as num?)?.toInt(), imageUrl: json['image_url'] as String?, ingredients: (json['ingredients'] as List?) ?.map((e) => RecipeIngredient.fromJson(e as Map)) .toList() ?? [], steps: (json['steps'] as List?) ?.map( (e) => RecipeStep.fromJson(e as Map)) .toList() ?? [], tags: (json['tags'] as List?) ?.map((e) => e as String) .toList() ?? [], savedAt: DateTime.tryParse(json['saved_at'] as String? ?? '') ?? DateTime.now(), caloriesPerServing: (json['calories_per_serving'] as num?)?.toDouble(), proteinPerServing: (json['protein_per_serving'] as num?)?.toDouble(), fatPerServing: (json['fat_per_serving'] as num?)?.toDouble(), carbsPerServing: (json['carbs_per_serving'] as num?)?.toDouble(), ); } Map toJson() => { 'id': id, 'recipe_id': recipeId, 'title': title, 'description': description, 'cuisine_slug': cuisine, 'difficulty': difficulty, 'prep_time_min': prepTimeMin, 'cook_time_min': cookTimeMin, 'servings': servings, 'image_url': imageUrl, 'ingredients': ingredients.map((e) => e.toJson()).toList(), 'steps': steps.map((e) => e.toJson()).toList(), 'tags': tags, 'saved_at': savedAt.toIso8601String(), 'calories_per_serving': caloriesPerServing, 'protein_per_serving': proteinPerServing, 'fat_per_serving': fatPerServing, 'carbs_per_serving': carbsPerServing, }; }