/* Written by retoor@molodetz.nl */

/*
This C extension for Python provides a simple API for communication with an
OpenAI service. It includes functions to return a "Hello World" string, conduct
a chat session through OpenAI, and reset the message history.
*/

/*
Summary of used imports:
- <Python.h>: Includes necessary Python headers to create a C extension.
- "openai.h": Assumes an external library for OpenAI interaction.
*/

/*
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.
*/

#define PY_SSIZE_T_CLEAN
#include "auth.h"
#include "openai.h"
#include <Python.h>

static PyObject *rpylib_reset(PyObject *self, PyObject *args) {
  return PyUnicode_FromString("True");
}

static PyObject *rpylib_chat(PyObject *self, PyObject *args) {
  const char *role, *message;

  if (!PyArg_ParseTuple(args, "ss", &role, &message)) {
    return NULL;
  }

  char *result = openai_chat(role, message);
  if (!result) {
    PyErr_SetString(PyExc_RuntimeError, "Failed to get response from OpenAI.");
    return NULL;
  }

  PyObject *py_result = PyUnicode_FromString(result);
  free(result);
  return py_result;
}

static PyObject *rpylib_prompt(PyObject *self, PyObject *args) {
  const char *role = "user";
  const char *message;
  if (!PyArg_ParseTuple(args, "s", &message)) {
    return NULL;
  }

  char *result = openai_chat(role, message);
  if (!result) {
    PyErr_SetString(PyExc_RuntimeError, "Failed to get response from OpenAI.");
    return NULL;
  }

  PyObject *py_result = PyUnicode_FromString(result);
  free(result);
  return py_result;
}

static PyObject *rpylib_system(PyObject *self, PyObject *args) {
  const char *role = "system";
  const char *message;
  if (!PyArg_ParseTuple(args, "s", &message)) {
    return NULL;
  }

  char *result = openai_chat(role, message);
  if (!result) {
    PyErr_SetString(PyExc_RuntimeError, "Failed to get response from OpenAI.");
    return NULL;
  }

  PyObject *py_result = PyUnicode_FromString(result);
  free(result);
  return py_result;
}

static PyMethodDef MyModuleMethods[] = {
    {"chat", rpylib_chat, METH_VARARGS, "Chat with OpenAI."},
    {"reset", rpylib_reset, METH_NOARGS, "Reset message history."},
    {NULL, NULL, 0, NULL}};

static struct PyModuleDef rpylib = {
    PyModuleDef_HEAD_INIT, "rpylib",
    "R - the power of R in Python, made by retoor.", -1, MyModuleMethods};

PyMODINIT_FUNC PyInit_rpylib(void) {
  auth_init();
  return PyModule_Create(&rpylib);
}