import 'dart:convert';

import '../../constant/links.dart';
import 'crud.dart';

class LlamaAi {
  Future<Map> getCarRegistrationData(String input, prompt) async {
    Map exrtatDataFinal = {};
    String oneLine = input.replaceAll('\n', ' ');
    // var res = await CRUD().getLlama(link: AppLink.gemini, payload: oneLine);
    var res = await CRUD()
        .getLlama(link: AppLink.llama, payload: oneLine, prompt: prompt);

    var decod = jsonDecode(res.toString());
    // exrtatDataFinal = jsonDecode(extractDataFromJsonString(decod['choices']));
    extractDataFromJsonString(decod['choices'][0]['message']['content']);
    return exrtatDataFinal;
  }

  String extractDataFromJsonString(String jsonString) {
    // Remove any leading or trailing whitespace from the string
    jsonString = jsonString.trim();

    // Extract the JSON substring from the given string
    final startIndex = jsonString.indexOf('{');
    final endIndex = jsonString.lastIndexOf('}');
    final jsonSubstring = jsonString.substring(startIndex, endIndex + 1);

    // Parse the JSON substring into a Map
    final jsonData = jsonDecode(jsonSubstring);

    // Return the extracted data

    return jsonEncode(jsonData);
  }
}
