"""
scripts/test.py
Script used for in-testing functions
Author: Vojtech Pokorny, vojtech.pokorny@prestonet.cz
Date (last edit): 6.2.2025
Note:
"""

import json

CONFIG_FILE = "/var/www/api/scripts/prestonet-webhooks-config.json"
_config = None  # cache for the loaded configuration

def load_config():
	"""
	Load the JSON configuration file.
	Returns the configuration as a dictionary.
	"""
	global _config
	if _config is None:
		try:
			with open(CONFIG_FILE, "r") as f:
				_config = json.load(f)
		except (IOError, json.JSONDecodeError) as e:
			print(f"Error loading config: {e}")
			_config = {}
	return _config

def get_admin_list():
	"""
	Returns the list of admin users from the configuration.
	"""
	config = load_config()
	return config.get("admins", [])

def get_user_group_list(user_email):
	"""
	Returns a list of all groups that the given user (email) is a member of.
	:param user_email: User's email address.
	:return: List of group names.
	"""
	config = load_config()
	groups = config.get("groups", {})
	user_groups = []
	
	# Check each group to see if the user email is in the list
	for group_name, emails in groups.items():
		if user_email in emails:
			user_groups.append(group_name)
			
	return user_groups
