'required']); if ($errors) { json_error('النص مطلوب', 422); } $apiKey = env('GEMINI_API_KEY'); if (!$apiKey) { json_error('Gemini API Key غير متوفر', 500); } $text = $data['text']; $systemPrompt = << [ ['parts' => [['text' => $text]]] ], 'systemInstruction' => [ 'parts' => [['text' => $systemPrompt]] ], 'generationConfig' => [ 'responseMimeType' => 'application/json', 'temperature' => 0.2 ] ]; // Determine appropriate endpoint based on env $model = env('GEMINI_MODEL', 'gemini-1.5-flash'); $url = "https://generativelanguage.googleapis.com/v1beta/models/{$model}:generateContent?key={$apiKey}"; $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($payload), CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => ['Content-Type: application/json'] ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); curl_close($ch); if ($httpCode !== 200) { error_log("Gemini Error: $response | $error"); json_error('فشل في تحليل الأمر', 500); } $respData = json_decode($response, true); if (!isset($respData['candidates'][0]['content']['parts'][0]['text'])) { json_error('رد غير متوقع من AI', 500); } $jsonText = $respData['candidates'][0]['content']['parts'][0]['text']; $parsed = json_decode($jsonText, true); if (!$parsed) { json_error('فشل في تحليل الرد كـ JSON', 500); } json_success($parsed, 'تم تحليل الأمر');