/****************************************************************************
 * 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 "./mdevices_init.h"

#include "aqhome-cgi/modules/mdataclient.h"

#include <aqcgi/service/module.h>

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


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

#define GBAS GWEN_Buffer_AppendString
#define GBAA GWEN_Buffer_AppendArgs

#define P_DEVICEREAD AQH_MODDEVICES_PERMS_DEVICEREAD
#define P_VALUEREAD  AQH_MODDEVICES_PERMS_VALUEREAD
#define P_VALUEWRITE AQH_MODDEVICES_PERMS_VALUEWRITE



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

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



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

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

  m=AQCGI_Module_new();
  AQCGI_Module_SetName(m, "devices");
  AQCGI_Module_SetDescr(m, "device 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, "DeviceRead",    0x001, "Read and list devices");
  AQH_ModService_AddPermDef(permDefList, "DeviceWrite",   0x002, "Modify devices");
  AQH_ModService_AddPermDef(permDefList, "DeviceAdd",     0x004, "Add devices");
  AQH_ModService_AddPermDef(permDefList, "DeviceDel",     0x008, "Remove devices");

  AQH_ModService_AddPermDef(permDefList, "ValueRead",     0x010, "Read and list values");
  AQH_ModService_AddPermDef(permDefList, "ValueWrite",    0x020, "Modify values");
  AQH_ModService_AddPermDef(permDefList, "ValueAdd",      0x040, "Add values");
  AQH_ModService_AddPermDef(permDefList, "ValueDel",      0x080, "Remove values");
  AQH_ModService_AddPermDef(permDefList, "ValueSet",      0x100, "Set values");

  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++, "Reader",
                         AQH_MODDEVICES_PERMS_DEVICEREAD |
                         AQH_MODDEVICES_PERMS_VALUEREAD,
                         "Read devices and values");
  AQH_ModService_AddRole(roleList, id++, "Writer",
                         AQH_MODDEVICES_PERMS_DEVICEREAD |
                         AQH_MODDEVICES_PERMS_DEVICEWRITE |
                         AQH_MODDEVICES_PERMS_DEVICEADD |
                         AQH_MODDEVICES_PERMS_DEVICEDEL |
                         AQH_MODDEVICES_PERMS_VALUEREAD |
                         AQH_MODDEVICES_PERMS_VALUEWRITE |
                         AQH_MODDEVICES_PERMS_VALUEADD |
                         AQH_MODDEVICES_PERMS_VALUEDEL |
                         AQH_MODDEVICES_PERMS_VALUESET,
                         "Read and write devices and values");
  AQH_ModService_AddRole(roleList, id++, "Setter",
                         AQH_MODDEVICES_PERMS_DEVICEREAD |
                         AQH_MODDEVICES_PERMS_VALUEREAD |
                         AQH_MODDEVICES_PERMS_VALUESET,
                         "Set values");
  AQCGI_Module_SetRoleList(m, roleList);
}




