using Seasoned.Backend.DTOs; using Mscc.GenerativeAI; using Microsoft.AspNetCore.Http; using System.IO; namespace Seasoned.Backend.Services; public class RecipeService : IRecipeService { private readonly string _apiKey; public RecipeService(IConfiguration config) { _apiKey = config["GeminiApiKey"] ?? throw new ArgumentNullException("API Key missing"); } public async Task ParseRecipeImageAsync(IFormFile image) { var googleAI = new GoogleAI(_apiKey); var model = googleAI.GenerativeModel("gemini-2.5-flash"); using var ms = new MemoryStream(); await image.CopyToAsync(ms); var base64Image = Convert.ToBase64String(ms.ToArray()); var prompt = "Extract the recipe from this image. Return Title and Description."; // 1. Initialize Content with the Role in the constructor var content = new Content(Role.User); // 2. Assign the parts using dynamic to bypass the IPart/Part conversion headache content.Parts = (dynamic)new List { new { text = prompt }, new { inline_data = new { mime_type = "image/png", data = base64Image } } }; // 3. Create the request var request = new GenerateContentRequest { Contents = new List { content } }; // 4. Call the model var response = await model.GenerateContent(request); return new RecipeResponseDto { Title = "Gemini 2.5 Analysis", Description = response.Text ?? "No text returned", Ingredients = new List(), Instructions = new List() }; } }