import { generateAIContent } from '../utils/aiHelper.js'; import { extractLanguage, getLanguageName } from '../utils/aiOrchestrator.js'; import dotenv from "dotenv"; export const cropRotationRecommendations = async (req, res) => { dotenv.config(); const { previousCrop, npkDepletion, waterAvailability, soilType, region } = req.body; if ( !previousCrop || !npkDepletion || !waterAvailability || !soilType || !region ) { return res.status(400).json({ error: "Missing required inputs: previousCrop, npkDepletion, waterAvailability, soilType, region", }); } const lang = extractLanguage(req); const langName = getLanguageName(lang); const langInstruction = lang !== 'en' ? `\n\nRespond STRICTLY in ${langName} language. Translate all fields and values.` : ''; try { const prompt = ` You are an expert agricultural crop rotation scientist. Based on the following: - Previous Crop: ${previousCrop} - NPK Depletion (major nutrient lost): ${npkDepletion} - Water Availability: ${waterAvailability} - Soil Type: ${soilType} - Region: ${region} Suggest the best crop to plant next to: - Restore depleted nutrients naturally - Increase soil fertility long-term - Improve economic profitability Provide the answer ONLY in this strict JSON format: { "recommended_crop": "", "reasons": ["", "", ""], "nutrient_restoration_benefit": "", "expected_profitability": "", "note": "" }${langInstruction} `; const recommendation = await generateAIContent(prompt.trim()); const formattedRecommendation = recommendation .replace("```json", "") .replace("```", "") .trim(); res.status(200).json({ recommendation: formattedRecommendation, }); } catch (err) { console.error("Error fetching recommendations: ", err); res.status(500).json({ error: "Failed to fetch recommendations" }); } };