/*
Espe | Ein schlichtes Werkzeug zur Mitglieder-Verwaltung | Backend
Copyright (C) 2024 Christian Fraß

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public 
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later 
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see 
<https://www.gnu.org/licenses/>. 
 */

namespace _espe.repository.member
{
	
	/**
	 */
	var _store : (
		null
		|
		lib_plankton.storage.type_store<
			_espe.type.member_id,
			Record<string, any>,
			{},
			lib_plankton.storage.type_sql_table_autokey_search_term,
			Record<string, any>
		>
	) = null;
	
	
	/**
	 */
	function get_store(
	) : lib_plankton.storage.type_store<
		_espe.type.member_id,
		Record<string, any>,
		{},
		lib_plankton.storage.type_sql_table_autokey_search_term,
		Record<string, any>
	>
	{
		if (_store === null) {
			_store = lib_plankton.storage.sql_table_autokey_store(
				{
					"database_implementation": _espe.helpers.database_implementation(),
					"table_name": "members",
					"key_name": "id",
				}
			);
		}
		else {
			// do nothing
		}	
		return _store;
	}
	
	
	/**
	 */
	function encode(
		object : _espe.type.member_object
	) : Record<string, any>
	{
		return {
			"membership_number": object.membership_number,
			"name_real_value": object.name_real_value,
			"name_real_index": object.name_real_index,
			"email_address_private": object.email_address_private,
			"registered": (object.registered ? 1 : 0),
			"enabled": (object.enabled ? 1 : 0),
			"email_use_veiled_address": (object.email_use_veiled_address ? 1 : 0),
			"email_use_nominal_address": (object.email_use_nominal_address ? 1 : 0),
			"email_redirect_to_private_address": (object.email_redirect_to_private_address ? 1 : 0),
			"email_allow_sending": (object.email_allow_sending ? 1 : 0),
			"password_image": object.password_image,
			"password_change_last_attempt": object.password_change_last_attempt,
			"password_change_token": object.password_change_token,
		};
	}
	
	
	/**
	 */
	function decode(
		row : Record<string, any>
	) : _espe.type.member_object
	{
		return {
			"membership_number": row["membership_number"],
			"name_real_value": row["name_real_value"],
			"name_real_index": row["name_real_index"],
			"email_address_private": row["email_address_private"],
			"registered": (row["registered"] > 0),
			"enabled": (row["enabled"] > 0),
			"email_use_veiled_address": (row["email_use_veiled_address"] > 0),
			"email_use_nominal_address": (row["email_use_nominal_address"] > 0),
			"email_redirect_to_private_address": (row["email_redirect_to_private_address"] > 0),
			"email_allow_sending": (row["email_allow_sending"] > 0),
			"password_image": row["password_image"],
			"password_change_last_attempt": row["password_change_last_attempt"],
			"password_change_token": row["password_change_token"],
		};
	}
	
	
	/**
	 */
	export async function dump(
	) : Promise<
		Array<
			{
				id : _espe.type.member_id;
				object : _espe.type.member_object;
			}
		>
	>
	{
		return (
			(await get_store().search(null))
			.map(
				({"key": key, "preview": preview}) => ({
					"id": key,
					"object": (preview as _espe.type.member_object),
				})
			)
		);
	}
	
	
	/**
	 * @todo optimize
	 */
	export async function list(
		search_term : (null | string)
	) : Promise<
		Array<
			{
				id : _espe.type.member_id;
				preview : {
					membership_number : string;
					name_real_value : string;
					name_real_index : int;
				};
			}
		>
	>
	{
		return (
			(await get_store().search(null))
			.filter(
				({"key": key, "preview": preview}) => (
					(
						(search_term === null)
						||
						(search_term.length <= 1)
					)
					? true
					: (
						preview["membership_number"].toLowerCase().includes(search_term.toLowerCase())
						||
						preview["name_real_value"].toLowerCase().includes(search_term.toLowerCase())
					)
				)
			)
			.map(
				({"key": key, "preview": preview}) => ({
					"id": key,
					"preview": {
						"membership_number": preview["membership_number"],
						"name_real_value": preview["name_real_value"],
						"name_real_index": preview["name_real_index"],
					}
				})
			)
		);
	}
	
	
	/**
	 */
	export async function read(
		id : _espe.type.member_id
	) : Promise<_espe.type.member_object>
	{
		const row : Record<string, any> = await get_store().read(id);
		
		return decode(row);
	}
	
	
	/**
	 */
	export async function create(
		value : _espe.type.member_object
	) : Promise<_espe.type.member_id>
	{
		const row : Record<string, any> = encode(value);
		const id : _espe.type.member_id = await get_store().create(row);
		
		return id;
	}
	
	
	/**
	 */
	export async function update(
		id : _espe.type.member_id,
		value : _espe.type.member_object
	) : Promise<void>
	{
		const row : Record<string, any> = encode(value);
		
		await get_store().update(id, row);
	}
	
	
	/**
	 */
	export async function delete_(
		id : _espe.type.member_id
	) : Promise<void>
	{
		await get_store().delete(id);
	}
	
}

