#ifndef R_OPENAI_H
#define R_OPENAI_H
#include "http.h"
#include "chat.h"
#include "http_curl.h"
#include <string.h>
#include <stdbool.h>

char* openai_fetch_models() {
    char* api_url = "https://api.openai.com/v1/models";
    return https_get(api_url);
}

bool openai_system(char* message_content) {
    (void)chat_json("system", message_content);
    return true;
}

struct json_object* openai_process_chat_message(char* api_url, char* json_data) {
    char* response = curl_post(api_url, json_data);
    struct json_object* parsed_json = json_tokener_parse(response);
    if (!parsed_json) {
        fprintf(stderr, "Failed to parse JSON.\n");
        fprintf(stderr, "%s\n", response);
        return json_object_new_null();
    }
    struct json_object *error_object;
    if(json_object_object_get_ex(parsed_json, "error", &error_object)) {
        fprintf(stderr, "Failed to get 'error' object.\n");
        fprintf(stderr, "%s\n", response);
        json_object_put(parsed_json);
        return json_object_new_null();
    }

    struct json_object* choices_array;
    if (!json_object_object_get_ex(parsed_json, "choices", &choices_array)) {
        fprintf(stderr, "Failed to get 'choices' array.\n");
        fprintf(stderr, "%s\n", response);
        json_object_put(parsed_json);
        return json_object_new_null();
    }
    struct json_object* first_choice = json_object_array_get_idx(choices_array, 0);
    if (!first_choice) {
        fprintf(stderr, "Failed to get the first element of 'choices'.\n");
        json_object_put(parsed_json);
        return json_object_new_null();
    }
    struct json_object* message_object;
    if (!json_object_object_get_ex(first_choice, "message", &message_object)) {
        fprintf(stderr, "Failed to get 'message' object.\n");
        json_object_put(parsed_json);
        return json_object_new_null();
    }
    return message_object;
}

char* openai_chat(char* user_role, char* message_content) {
    char* api_url = "https://api.openai.com/v1/chat/completions";
    char* json_data = chat_json(user_role, message_content);
    json_object* message_object = openai_process_chat_message(api_url, json_data);
    struct json_object* tool_calls;
    json_object_object_get_ex(message_object, "tool_calls", &tool_calls);
    if (tool_calls) {
        message_add_tool_call(message_object);
        json_object* tool_call_results = tools_execute(tool_calls);
        int results_count = json_object_array_length(tool_call_results);
        for (int i = 0; i < results_count; i++) {
            json_object* tool_call_result = json_object_array_get_idx(tool_call_results, i);
            message_add_tool_call(tool_call_result);
        }
        char* tool_calls_result_str = chat_json(NULL, NULL);
        message_object = openai_process_chat_message(api_url, tool_calls_result_str);
        message_add_tool_call(message_object);
    }
    char* content_str = (char*)json_object_get_string(json_object_object_get(message_object, "content"));
    return strdup(content_str);
}

#endif