// Written by retoor@molodetz.nl

// This code defines a simple HTTP client using libcurl in C. It provides
// functions for executing POST and GET HTTP requests with JSON data, including
// authorization via a bearer token. The functions `curl_post` and `curl_get`
// handle these operations and return the server's response as a string.

// Uses libcurl for HTTP requests and includes a custom "auth.h" for API key
// resolution.

// 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 HTTP_CURL
#define HTTP_CURL

#include "auth.h"
#include <curl/curl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

struct ResponseBuffer {
  char *data;
  size_t size;
};

static size_t WriteCallback(void *contents, size_t size, size_t nmemb,
                            void *userp) {
  size_t total_size = size * nmemb;
  struct ResponseBuffer *response = (struct ResponseBuffer *)userp;

  if (total_size > SIZE_MAX - response->size - 1) {
    fprintf(stderr, "Response too large\n");
    return 0;
  }

  char *ptr = realloc(response->data, response->size + total_size + 1);
  if (ptr == NULL) {
    fprintf(stderr, "Failed to allocate memory for response\n");
    return 0;
  }
  response->data = ptr;
  memcpy(&(response->data[response->size]), contents, total_size);
  response->size += total_size;
  response->data[response->size] = '\0';
  return total_size;
}

#define HTTP_MAX_RETRIES 3
#define HTTP_RETRY_DELAY_MS 2000

char *curl_post(const char *url, const char *data) {
  CURL *curl;
  CURLcode res;
  struct ResponseBuffer response = {NULL, 0};
  int retry_count = 0;

  while (retry_count < HTTP_MAX_RETRIES) {
    if (response.data) {
      free(response.data);
    }
    response.data = malloc(1);
    response.size = 0;
    if (!response.data) {
      return NULL;
    }
    response.data[0] = '\0';

    curl = curl_easy_init();
    if (!curl) {
      free(response.data);
      return NULL;
    }

    struct curl_slist *headers = NULL;
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 300L);
    headers = curl_slist_append(headers, "Content-Type: application/json");
    char bearer_header[1337];
    snprintf(bearer_header, sizeof(bearer_header), "Authorization: Bearer %s",
             resolve_api_key());
    headers = curl_slist_append(headers, bearer_header);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);

    res = curl_easy_perform(curl);
    curl_slist_free_all(headers);
    curl_easy_cleanup(curl);

    if (res == CURLE_OK) {
      return response.data;
    }

    retry_count++;
    fprintf(stderr, "Network error: %s (attempt %d/%d)\n",
            curl_easy_strerror(res), retry_count, HTTP_MAX_RETRIES);

    if (retry_count < HTTP_MAX_RETRIES) {
      fprintf(stderr, "Retrying in %d seconds...\n", HTTP_RETRY_DELAY_MS / 1000);
      usleep(HTTP_RETRY_DELAY_MS * 1000);
    }
  }

  fprintf(stderr, "Failed after %d attempts.\n", HTTP_MAX_RETRIES);
  free(response.data);
  return NULL;
}

char *curl_get(const char *url) {
  CURL *curl;
  CURLcode res;
  struct ResponseBuffer response = {NULL, 0};
  int retry_count = 0;

  while (retry_count < HTTP_MAX_RETRIES) {
    if (response.data) {
      free(response.data);
    }
    response.data = malloc(1);
    response.size = 0;
    if (!response.data) {
      return NULL;
    }
    response.data[0] = '\0';

    curl = curl_easy_init();
    if (!curl) {
      free(response.data);
      return NULL;
    }

    struct curl_slist *headers = NULL;
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60L);
    headers = curl_slist_append(headers, "Content-Type: application/json");
    char bearer_header[1337];
    snprintf(bearer_header, sizeof(bearer_header), "Authorization: Bearer %s",
             resolve_api_key());
    headers = curl_slist_append(headers, bearer_header);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);

    res = curl_easy_perform(curl);
    curl_slist_free_all(headers);
    curl_easy_cleanup(curl);

    if (res == CURLE_OK) {
      return response.data;
    }

    retry_count++;
    fprintf(stderr, "Network error: %s (attempt %d/%d)\n",
            curl_easy_strerror(res), retry_count, HTTP_MAX_RETRIES);

    if (retry_count < HTTP_MAX_RETRIES) {
      fprintf(stderr, "Retrying in %d seconds...\n", HTTP_RETRY_DELAY_MS / 1000);
      usleep(HTTP_RETRY_DELAY_MS * 1000);
    }
  }

  fprintf(stderr, "Failed after %d attempts.\n", HTTP_MAX_RETRIES);
  free(response.data);
  return NULL;
}

#endif