/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2025 Martin Preuss, all rights reserved.
 *
 * The license for this file can be found in the file COPYING which you
 * should have received along with this file.
 ****************************************************************************/

#ifndef AQHOME_CGI_MSERVICE_H
#define AQHOME_CGI_MSERVICE_H

#include <aqcgi/service/module.h>
#include <aqcgi/service/service.h>
#include <aqcgi/service/session.h>

#include <aqcgi/request.h>

#include <gwenhywfar/buffer.h>


#define AQH_MODSERVICE_RQFLAGS_RAWFILE 0x10000000



typedef int (*AQH_MODSERVICE_HANDLEREQUEST_FN)(AQCGI_MODULE *m, AQCGI_REQUEST *rq, AQCGI_SESSION *session, const char *sLastPathElem);
typedef AQCGI_MODULE* (*AQH_MODSERVICE_LOADSUBMODULE_FN)(AQCGI_MODULE *m, AQCGI_REQUEST *rq, AQCGI_SESSION *session, const char *sModuleName);
typedef void (*AQH_MODSERVICE_ADDHEADER_FN)(AQCGI_MODULE *m, const char *lang, GWEN_BUFFER *dbuf);
typedef void (*AQH_MODSERVICE_ADDFOOTER_FN)(AQCGI_MODULE *m, const char *lang, GWEN_BUFFER *dbuf);


typedef struct AQH_MODSERVICE_HANDLER_ENTRY AQH_MODSERVICE_HANDLER_ENTRY;
struct AQH_MODSERVICE_HANDLER_ENTRY {
  const char *page;
  int httpMethod;
  uint32_t perms;
  void (*handlerFn)(AQCGI_MODULE *m, AQCGI_REQUEST *rq, AQCGI_SESSION *session, GWEN_BUFFER *dbuf);
};



void AQH_ModService_Extend(AQCGI_MODULE *m, AQCGI_SERVICE *sv, const char *baseFolder);

AQCGI_SERVICE *AQH_ModService_GetService(const AQCGI_MODULE *m);
const char *AQH_ModService_GetBaseFolder(const AQCGI_MODULE *m);

uint32_t AQH_ModService_GetUserPerms(const AQCGI_MODULE *m);

void AQH_ModService_AddHeader(AQCGI_MODULE *m, const char *lang, GWEN_BUFFER *dbuf);
void AQH_ModService_AddFooter(AQCGI_MODULE *m, const char *lang, GWEN_BUFFER *dbuf);

AQCGI_MODULE *AQH_ModService_LoadSubModule(AQCGI_MODULE *m, AQCGI_REQUEST *rq, AQCGI_SESSION *session, const char *sModuleName);
int AQH_ModService_HandleRequest(AQCGI_MODULE *m, AQCGI_REQUEST *rq, AQCGI_SESSION *session, const char *sLastPathElem);

AQCGI_SESSION *AQH_ModService_ReadSession(AQCGI_MODULE *m, AQCGI_REQUEST *rq);
void AQH_ModService_CalcSessionModPerms(AQCGI_MODULE *m, const AQCGI_SESSION *session);

void AQH_ModService_HandleRequestWithTable(AQCGI_MODULE *m,
                                           AQCGI_REQUEST *rq,
                                           AQCGI_SESSION *session,
                                           const char *page,
                                           const AQH_MODSERVICE_HANDLER_ENTRY *e);

int AQH_ModService_RespondWithFile(AQCGI_MODULE *m, AQCGI_REQUEST *rq, const char *lang, const char *sFilename, GWEN_BUFFER *dbuf);
int AQH_ModService_ReadStaticFile(AQCGI_MODULE *m, const char *lang, const char *filename, GWEN_BUFFER *dbuf);

void AQH_ModService_AddPermDef(AQCGI_PERMDEF_LIST *permDefList, const char *id, uint32_t mask, const char *descr);
void AQH_ModService_AddRole(AQCGI_ROLE_LIST *roleList, int id, const char *name, uint32_t perms, const char *descr);


void AQH_ModService_WritePermsToForm(uint32_t perms, const AQCGI_PERMDEF_LIST *permDefList, const char *sPrefix, GWEN_BUFFER *dbuf);
uint32_t AQH_ModService_ReadPermsFromForm(GWEN_DB_NODE *dbPost, const AQCGI_PERMDEF_LIST *permDefList, const char *sPrefix);

AQCGI_MODULE_LIST *AQH_ModService_LoadRawModules(AQCGI_MODULE *m);
AQCGI_USER_LIST *AQH_ModService_LoadRawUsers(AQCGI_MODULE *m);


void AQH_ModService_SetHandleRequestFn(AQCGI_MODULE *m, AQH_MODSERVICE_HANDLEREQUEST_FN fn);
void AQH_ModService_SetLoadSubModuleFn(AQCGI_MODULE *m, AQH_MODSERVICE_LOADSUBMODULE_FN fn);

void AQH_ModService_SetAddHeaderFn(AQCGI_MODULE *m, AQH_MODSERVICE_ADDHEADER_FN fn);
void AQH_ModService_SetAddFooterFn(AQCGI_MODULE *m, AQH_MODSERVICE_ADDFOOTER_FN fn);

void AQH_ModService_EscapeToBuffer(const char *src, GWEN_BUFFER *buf);
void AQH_ModService_UnescapeToBuffer(const char *src, GWEN_BUFFER *buf);

int AQH_ModService_FileIsCurrent(const char *sPath, int seconds);

int AQH_ModService_RespondWithMimeFile(AQCGI_REQUEST *rq, const char *sFilename, const char *sMimeType, GWEN_BUFFER *dbuf);
int AQH_ModService_RespondWithVarFile(AQCGI_MODULE *m, AQCGI_REQUEST *rq,
                                      const char *lang,
                                      const char *sFilename,
                                      GWEN_DB_NODE *dbVars,
                                      GWEN_BUFFER *dbuf);

int AQH_ModService_AddRequestUrl(const AQCGI_REQUEST *rq, GWEN_BUFFER *dbuf);
void AQH_ModService_Redirect(AQCGI_REQUEST *rq, const char *destLocation);
void AQH_ModService_RedirectWithReferer(AQCGI_REQUEST *rq, const char *destLocation);
void AQH_ModService_DenyRequest(AQCGI_MODULE *m, AQCGI_REQUEST *rq, const AQCGI_SESSION *session, GWEN_BUFFER *dbuf);

#endif

