// Written by retoor@molodetz.nl

// This code manages a collection of messages using JSON objects. It provides
// functions to retrieve all messages as a JSON array, add a new message with a
// specified role and content, and free the allocated resources.

// Uses the external library <json-c/json.h> for JSON manipulation

// MIT License
// 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_MESSAGES_H
#define R_MESSAGES_H

#include "db_utils.h"
#include "json-c/json.h"
#include "tools.h"
#include <string.h>
struct json_object *message_array = NULL;

struct json_object *message_list() {
  if (!message_array) {
    message_array = json_object_new_array();
  }
  return message_array;
}
bool messages_remove_last() {
  struct json_object *messages = message_list();
  int size = json_object_array_length(messages);
  if (size) {
    json_object_array_del_idx(messages, size - 1, 1);
    return true;
  }
  return false;
}

void messages_remove() {
  while (messages_remove_last())
    continue;
}

struct json_object *message_add_tool_call(struct json_object *message) {
  struct json_object *messages = message_list();
  json_object_array_add(messages, message);
  return message;
}

struct json_object *message_add_tool_result(const char *tool_call_id,
                                            char *tool_result) {
  struct json_object *messages = message_list();
  struct json_object *message = json_object_new_object();

  json_object_object_add(message, "tool_call_id",
                         json_object_new_string(tool_call_id));

  if (strlen(tool_result) > 104000) {
    tool_result[104000] = '\0';
  }

  json_object_object_add(message, "tool_result",
                         json_object_new_string(tool_result));

  json_object_array_add(messages, message);
  return message;
}

void message_add_object(json_object *message) {
  struct json_object *messages = message_list();
  json_object_array_add(messages, message);
}

struct json_object *message_add(const char *role, const char *content);

struct json_object *message_add(const char *role, const char *content) {
  struct json_object *messages = message_list();
  struct json_object *message = json_object_new_object();
  json_object_object_add(message, "role", json_object_new_string(role));

  if (content) {
    char *formatted_content = strdup(content);
    if (strlen(formatted_content) > 1048570) {
      formatted_content[1048570] = '\0';
    }
    json_object_object_add(message, "content",
                           json_object_new_string(formatted_content));
    free(formatted_content);
  }
  if (!strcmp(role, "user")) {
    json_object_object_add(message, "tools", tools_descriptions());
    json_object_object_add(message, "parallel_tool_calls",
                           json_object_new_boolean(true));
  }

  json_object_array_add(messages, message);
  return message;
}

char *message_json() {
  return (char *)json_object_to_json_string_ext(message_list(),
                                                JSON_C_TO_STRING_PRETTY);
}

void message_free() {
  if (message_array) {
    json_object_put(message_array);
    message_array = NULL;
  }
}

#endif
