import 'dart:convert';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;

class GeminiEgypt extends GetxController {
  Map<String, dynamic> responseIdCardDriverEgypt = {};
  String? responseIdCardDriverEgypt1;

  Future geminiAiExtraction(String prompt, payload) async {
    var requestBody = jsonEncode({
      'contents': [
        {
          'parts': [
            // {
            //   'inlineData': {
            //     'mimeType': 'image/jpeg',
            //     'data': imageData,
            //   },
            // },
            {
              'text': """
        $payload
        
         $prompt ,and make dates format like year-month-day"""
            },
          ],
        },
      ],
      'generationConfig': {
        'temperature': 0.4,
        'topK': 32,
        'topP': 1,
        'maxOutputTokens': 4096,
        'stopSequences': [],
      },
      'safety_settings': [
        {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
        {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
        {
          "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
          "threshold": "BLOCK_NONE"
        },
        {
          "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
          "threshold": "BLOCK_NONE"
        },
      ]
    });

    final response = await http.post(
      Uri.parse(
          // 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro-vision:generateContent?key=${AK.geminiApi}'),
          'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=AIzaSyCyoLcSkDzK5_SMe00nhut56SSXWPR074w'),
      headers: {'Content-Type': 'application/json'},
      body: requestBody,
    );

    if (response.statusCode == 200) {
      var responseData = jsonDecode(response.body);
      // Process the responseData as needed

      var result = responseData['candidates'][0]['content']['parts'][0]['text'];
      RegExp regex = RegExp(r"```json([^`]*)```");
      String? jsonString =
          regex.firstMatch(responseData.toString())?.group(1)?.trim();

      if (jsonString != null) {
        // Convert the JSON object to a String
        jsonString = jsonEncode(json.decode(jsonString));
        responseIdCardDriverEgypt1 = jsonString;

        responseIdCardDriverEgypt = jsonDecode(responseIdCardDriverEgypt1!);
        update();
        return responseIdCardDriverEgypt;
      } else {}

      // Rest of your code...
    } else {}
  }
}
