// Written by retoor@molodetz.nl

// This code interacts with OpenAI's API to perform various tasks such as
// fetching models, sending chat messages, and processing responses.

// Uncommon imports include "http.h", "chat.h", and "http_curl.h". These may be
// internal or external libraries providing HTTP and JSON communication
// capabilities required to interact with APIs.

// MIT License
//
// Copyright (c) 2023
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

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

char *openai_fetch_models() { return curl_get(get_models_api_url()); }

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

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

    char *all_messages = (char *)json_object_to_json_string(message_array);

    fprintf(stderr, "Messages: ");
    fwrite(all_messages, strlen(all_messages), 1, stderr);
    fprintf(stderr, "\n");
    free(all_messages);

    fprintf(stderr, "%s\n", json_object_to_json_string(parsed_json));

    json_object_put(parsed_json);
    messages_remove_last();

    messages_remove_last();

    return 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%s\n", response);
    fprintf(stderr, "%s\n", json_object_to_json_string(parsed_json));
    json_object_put(parsed_json);
    return 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 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 NULL;
  }
  return message_object;
}

char *openai_chat(const char *user_role, const char *message_content) {
  if (message_content == NULL || *message_content == '\0' ||
      *message_content == '\n') {
    return NULL;
  }

  char *json_data = chat_json(user_role, message_content);

  struct json_object *message_object =
      openai_process_chat_message(get_completions_api_url(), json_data);

  if (message_object == NULL) {
    return NULL;
  }

  message_add_object(message_object);
  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);
    struct 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++) {
      struct 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(get_completions_api_url(),
                                                 tool_calls_result_str);
    if (message_object == NULL) {
      return NULL;
    }
    message_add_object(message_object);
    // message_add_tool_call(message_object);
  }
  const char *content_str =
      json_object_get_string(json_object_object_get(message_object, "content"));
  return strdup(content_str);
}

#endif
