// Written by retoor@molodetz.nl

// This code defines functionality for creating and managing JSON-based chat
// prompts using a specific AI model configuration, providing easy integration
// with message handling and HTTP communication for dynamic applications.

// Non-standard imports: json-c library for handling JSON objects.

// 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_PROMPT_H
#define R_PROMPT_H

#include "auth.h"
#include "messages.h"
#include "r.h"
#include "tools.h"
#include <json-c/json.h>
static json_object *_prompt = NULL;

void chat_free() {
  if (_prompt == NULL)
    return;
  json_object_put(_prompt);
  _prompt = NULL;
}

char *chat_json(const char *role, const char *message) {
  chat_free();
  json_object *root_object = json_object_new_object();
  json_object_object_add(root_object, "model",
                         json_object_new_string(get_prompt_model()));

  if (role != NULL && message != NULL) {
    message_add(role, message);
    json_object_object_add(root_object, "tools", tools_descriptions());
  }

  json_object_object_add(root_object, "messages", message_list());
  //    json_object_object_add(root_object, "max_tokens",
  //    json_object_new_int(prompt_max_tokens));
  json_object_object_add(root_object, "temperature",
                         json_object_new_double(PROMPT_TEMPERATURE));

  return (char *)json_object_to_json_string_ext(root_object,
                                                JSON_C_TO_STRING_PRETTY);
}

#endif
