import 'dart:convert';

import 'package:SEFER/constant/links.dart';
import 'package:SEFER/controller/functions/crud.dart';

class LlamaAi {
  Future<Map> getCarRegistrationData(String input) async {
    print(true);
    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);
    var decod = jsonDecode(res);
    // print(decod);
    exrtatDataFinal = jsonDecode(
        extractDataFromJsonString(decod['choices'][0]['message']['content']));
    // print(jsonEncode(exrtatDataFinal));
    print(false);
    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

    // print(jsonEncode(jsonData));
    return jsonEncode(jsonData);
  }
}
