#include "seobeo/seobeo.h"

#include <unistd.h>

static void Send_Later(void *p_context)
{
  Seobeo_SSE_Stream *p_stream = p_context;
  usleep(100000);
  if (Seobeo_SSE_Is_Open(p_stream))
    Seobeo_SSE_Send_Data(p_stream, "later");
}

static void Release_Stream(void *p_context)
{
  Seobeo_SSE_Release(p_context);
}

static void Events(
    Seobeo_SSE_Stream *p_stream,
    Seobeo_Request_Entry *p_request,
    Dowa_Arena *p_arena)
{
  (void)p_request;
  (void)p_arena;
  Seobeo_SSE_Send_Data(p_stream, "connected");
  if (Seobeo_SSE_Retain(p_stream))
  {
    Seobeo_Worker_Result result = Seobeo_Thread_Start_Detached(
        Send_Later,
        p_stream,
        Release_Stream);
    if (result != SEOBEO_WORKER_OK)
      Seobeo_SSE_Release(p_stream);
  }
}

static Seobeo_Request_Entry *Health(
    Seobeo_Request_Entry *p_request,
    Dowa_Arena *p_arena)
{
  (void)p_request;
  Seobeo_Request_Entry *p_response = NULL;
  Dowa_HashMap_Push_Arena(p_response, "status", "200", p_arena);
  Dowa_HashMap_Push_Arena(
      p_response,
      "content-type",
      "text/plain",
      p_arena);
  Dowa_HashMap_Push_Arena(p_response, "body", "ok", p_arena);
  return p_response;
}

int main(int argc, char **argv)
{
  const char *port = argc > 1 ? argv[1] : "18081";
  Seobeo_Router_Init();
  Seobeo_Router_Register_SSE("/events", Events);
  Seobeo_Router_Register("GET", "/health", Health);
  int result = Seobeo_Web_Server_Start_On(
      "127.0.0.1",
      "seobeo/examples",
      port,
      SEOBEO_MODE_EDGE,
      1);
  Seobeo_Router_Destroy();
  return result;
}
