File size: 1,740 Bytes
8a6248c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { generateAIContent } from '../utils/aiHelper.js';
import { extractLanguage, getLanguageName } from '../utils/aiOrchestrator.js';
import dotenv from 'dotenv';

export const getWaterOptimizations = async (req, res) => {
    dotenv.config();
    
    const { weather, soilMoisture, cropStage, evaporationRate } = req.body;

    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 irrigation scientist.

        Based on the following details:
        - Weather: ${weather}
        - Soil Moisture (%): ${soilMoisture}
        - Crop Stage: ${cropStage}
        - Evaporation Rate (mm/day): ${evaporationRate}

        Calculate:

        1. Recommended water requirement per day (in liters per plant OR mm per acre — choose best based on input).
        2. Expected water savings (%) compared to traditional irrigation.
        3. A short explanation (1–2 lines).

        Respond ONLY in this JSON structure:

        {
            "water_required": "value + units",
            "water_saving_percent": number,
            "note": "short explanation"
        }${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" });
    }
};