/*
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.sample
{
	
	/**
	 */
	type type_data = {
		admins : Array<
			{
				name : string;
				email_address : (null | string);
				password : string;
			}
		>;
		members : Array<
			{
				membership_number : string;
				name_real : string;
				email_address_private : (null | string);
				groups : Array<string>;
			}
		>;
		invites : Array<
			{
				membership_number_mode : _espe.type.invite_prefill_mode_raw;
				membership_number_value : string;
				name_mode : _espe.type.invite_prefill_mode_raw;
				name_value : string;
				email_address_mode : _espe.type.invite_prefill_mode_raw;
				email_address_value : (null | string);
				groups_mode : _espe.type.invite_prefill_mode_raw;
				groups_value : Array<string>;
			}
		>;
	};

	
	/**
	 */
	export async function fill(
		data : type_data
	) : Promise<void>
	{
		// admins
		{
			for (const admin_raw of data.admins) {
				const admin_id : _espe.type.admin_id = await _espe.service.admin.add(
					admin_raw.name,
					admin_raw.email_address,
					admin_raw.password,
				);
			}
		}
		// members
		{
			for (const member_raw of data.members) {
				const member_id : _espe.type.member_id = await _espe.service.member.project(
					{
						"membership_number": member_raw.membership_number,
						"name_real_value": member_raw.name_real,
						"email_address_private": member_raw.email_address_private,
						"groups": member_raw.groups
					},
					{
						"silent": true,
					}
				);
			}
			/**
			 * @todo passwords
			 */
		}
		// invites
		{
			for (const invite_raw of data.invites) {
				const result : {id : _espe.type.invite_id; key : _espe.type.invite_key;} = await _espe.service.invite.create(
					{
						"membership_number_mode": _espe.helpers.invite_prefill_mode_decode(invite_raw.membership_number_mode),
						"membership_number_value": invite_raw.membership_number_value,
						"name_mode": invite_raw.name_mode,
						"name_value": invite_raw.name_value,
						"email_address_mode": invite_raw.email_address_mode,
						"email_address_value": invite_raw.email_address_value,
						"groups_mode": invite_raw.groups_mode,
						"groups_value": invite_raw.groups_value,
					}
				);
			}
		}
	}


	/**
	 */
	export async function fill_by_path(
		path : string
	) : Promise<void>
	{
		const content : string = await lib_plankton.file.read(path);
		const data : type_data = (lib_plankton.json.decode(content) as type_data);
		await fill(data);
	}

}

