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

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif


#include "./madmin.h"

#include "aqhome-cgi/modules/common/mmodules.h"
#include "aqhome-cgi/modules/common/musers.h"

#include <aqcgi/service/module.h>

#include <gwenhywfar/debug.h>
#include <gwenhywfar/timestamp.h>



/* ------------------------------------------------------------------------------------------------
 * defs and enums
 * ------------------------------------------------------------------------------------------------
 */

/* ------------------------------------------------------------------------------------------------
 * global vars
 * ------------------------------------------------------------------------------------------------
 */

/* ------------------------------------------------------------------------------------------------
 * forward declarations
 * ------------------------------------------------------------------------------------------------
 */

static void _createPermDefList(AQCGI_MODULE *m);
static void _createRoleList(AQCGI_MODULE *m);

static AQCGI_MODULE *_loadSubModule(AQCGI_MODULE *m, AQCGI_REQUEST *rq, AQCGI_SESSION *session, const char *sModuleName);
static int _handleRequest(AQCGI_MODULE *m, AQCGI_REQUEST *rq, AQCGI_SESSION *session, const char *sLastPathElem);
static int _handleRqIndex(AQCGI_MODULE *m, AQCGI_REQUEST *rq, GWEN_BUFFER *dbuf);


/* ------------------------------------------------------------------------------------------------
 * code
 * ------------------------------------------------------------------------------------------------
 */

void AQH_ModAdmin_Extend(AQCGI_MODULE *m, AQCGI_SERVICE *sv, const char *baseFolder)
{
  AQH_ModService_Extend(m, sv, baseFolder);
  AQH_ModService_SetHandleRequestFn(m, _handleRequest);
  AQH_ModService_SetLoadSubModuleFn(m, _loadSubModule);
}



int AQH_ModAdmin_Create(AQCGI_SERVICE *sv)
{
  AQCGI_MODULE *m;
  int rv;

  m=AQCGI_Module_new();
  AQCGI_Module_SetName(m, "admin");
  AQCGI_Module_SetDescr(m, "administration module");
  AQCGI_Module_SetGuestPerms(m, 0);

  _createPermDefList(m);
  _createRoleList(m);

  rv=AQCGI_Service_AddModule(sv, m);
  if (rv<0) {
    DBG_INFO(NULL, "here (%d)", rv);
  }
  AQCGI_Module_free(m);
  return rv;
}



void _createPermDefList(AQCGI_MODULE *m)
{
  AQCGI_PERMDEF_LIST *permDefList;

  permDefList=AQCGI_PermDef_List_new();

  AQH_ModService_AddPermDef(permDefList, "AdminUsers",    0x001, "User Administration");
  AQH_ModService_AddPermDef(permDefList, "AdminModules",  0x002, "Module Administration");

  AQCGI_Module_SetPermDefList(m, permDefList);
}



void _createRoleList(AQCGI_MODULE *m)
{
  AQCGI_ROLE_LIST *roleList;
  int id=1;

  roleList=AQCGI_Role_List_new();
  AQH_ModService_AddRole(roleList, id++, "userAdmin",   AQH_MODADM_PERMS_ADMINUSERS,   "User administrator");
  AQH_ModService_AddRole(roleList, id++, "moduleAdmin", AQH_MODADM_PERMS_ADMINMODULES, "Module administrator");
  AQCGI_Module_SetRoleList(m, roleList);
}



AQCGI_MODULE *_loadSubModule(AQCGI_MODULE *m, AQCGI_REQUEST *rq, AQCGI_SESSION *session, const char *sModuleName)
{
  AQCGI_SERVICE *sv;

  sv=AQH_ModService_GetService(m);
  if (strcasecmp(sModuleName, "modules")==0) {
    AQCGI_MODULE *mSub;

    mSub=AQCGI_Service_LoadModule(sv, sModuleName);
    if (mSub) {
      const char *s;
      GWEN_BUFFER *nbuf;

      nbuf=GWEN_Buffer_new(0, 256, 0, 1);
      s=AQH_ModService_GetBaseFolder(m);
      GWEN_Buffer_AppendArgs(nbuf, "%s/modules", s?s:".");

      AQH_ModAdmModules_Extend(mSub, AQH_ModService_GetService(m), GWEN_Buffer_GetStart(nbuf));
      AQCGI_Module_Tree2_AddChild(m, mSub);
      GWEN_Buffer_free(nbuf);
      return mSub;
    }
  }
  else if (strcasecmp(sModuleName, "users")==0) {
    AQCGI_MODULE *mSub;

    mSub=AQCGI_Service_LoadModule(sv, sModuleName);
    if (mSub) {
      const char *s;
      GWEN_BUFFER *nbuf;

      nbuf=GWEN_Buffer_new(0, 256, 0, 1);
      s=AQH_ModService_GetBaseFolder(m);
      GWEN_Buffer_AppendArgs(nbuf, "%s/modules", s?s:".");

      AQH_ModAdmUsers_Extend(mSub, AQH_ModService_GetService(m), GWEN_Buffer_GetStart(nbuf));
      AQCGI_Module_Tree2_AddChild(m, mSub);
      GWEN_Buffer_free(nbuf);
      return mSub;
    }
  }
  return NULL;
}



int _handleRequest(AQCGI_MODULE *m, AQCGI_REQUEST *rq, AQCGI_SESSION *session, const char *sLastPathElem)
{
  GWEN_BUFFER *dbuf;
  int rv=0;

  dbuf=GWEN_Buffer_new(0, 256, 0, 1);
  AQH_ModService_AddHeader(m, "en", dbuf);

  if (strcasecmp(sLastPathElem, "index.html")==0) {
    if (AQH_ModService_GetUserPerms(m) & (AQH_MODADM_PERMS_ADMINUSERS | AQH_MODADM_PERMS_ADMINMODULES))
      rv=_handleRqIndex(m, rq, dbuf);
    else
      AQH_ModService_DenyRequest(m, rq, session, dbuf);
  }
  else {
    AQCGI_Request_SetResponseCode(rq, 404);
    AQCGI_Request_SetResponseText(rq, "Not Found");
  }
  AQH_ModService_AddFooter(m, "en", dbuf);
  AQCGI_Request_SetBufferResponseBody(rq, dbuf);
  AQCGI_Request_AddResponseHeaderData(rq, "Content-type: text/html");

  return AQCGI_SendResponse(rq);
}



int _handleRqIndex(AQCGI_MODULE *m, AQCGI_REQUEST *rq, GWEN_BUFFER *dbuf)
{
  if (AQCGI_Request_GetRequestMethod(rq)==AQCGI_REQUEST_METHOD_GET) {
    uint32_t userPerms;

    GWEN_Buffer_AppendString(dbuf, "<table>");
    userPerms=AQH_ModService_GetUserPerms(m);
    if (userPerms & AQH_MODADM_PERMS_ADMINUSERS)
      GWEN_Buffer_AppendString(dbuf,
                               "<tr>"
                               "<td><a href=\"users/index.html\" >User administration</a></td>"
                               "<td>Add, remove or modify users</td>"
                               "</tr>\n");
    if (userPerms & AQH_MODADM_PERMS_ADMINMODULES)
      GWEN_Buffer_AppendString(dbuf,
                               "<tr>"
                               "<td><a href=\"modules/index.html\" >Module administration</a></td>"
                               "<td>Add, remove or modify modules</td>"
                               "</tr>\n");
    GWEN_Buffer_AppendString(dbuf, "</table>\n");
    AQCGI_Request_SetResponseCode(rq, 200);
    AQCGI_Request_SetResponseText(rq, "Ok");
    return 0;
  }
  AQCGI_Request_SetResponseCode(rq, 405);
  AQCGI_Request_SetResponseText(rq, "Method Not Allowed");
  return 0;
}



