/*
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.api
{
	
	/**
	 */
	export function register_invite_create(
		rest_subject : lib_plankton.rest_http.type_rest
	) : void
	{
		lib_plankton.rest_http.register<
			{
				membership_number_mode : int;
				membership_number_value : (null | string);
				name_mode : int;
				name_value : string;
				email_address_mode : int;
				email_address_value : (null | string);
				groups_mode : int;
				groups_value : Array<string>;
				expiry ?: (null | int);
				// notification_target_url_template ?: (null | string);
			},
			(
				string
				|
				{
					id : _espe.type.member_id;
					key : string;
				}
			)
		>(
			rest_subject,
			lib_plankton.http.enum_method.post,
			_espe.api.full_path("/invite/create"),
			{
				/**
				 * @todo translation
				 */
				"description": () => "erstellt eine Einladung und gibt die erzeugte ID und den erzeugten Schlüssel aus",
				"input_schema": () => ({
					"type": "object",
					"nullable": false,
					"additionalProperties": false,
					"properties": {
						"membership_number_mode": {
							"type": "integer",
							"nullable": false,
							"description": "Mitgliedsnummer | Modus"
						},
						"membership_number_value": {
							"type": "string",
							"nullable": true,
							"description": "Mitgliedsnummer | Wert"
						},
						"name_mode": {
							"type": "integer",
							"nullable": false,
							"description": "Name | Modus"
						},
						"name_value": {
							"type": "string",
							"nullable": true,
							"description": "Name | Wert"
						},
						"email_address_mode": {
							"type": "integer",
							"nullable": true,
							"description": "E-Mail-Adresse | Modus"
						},
						"email_address_value": {
							"type": "string",
							"nullable": true,
							"description": "E-Mail-Adresse | Wert"
						},
						"groups_integer": {
							"type": "integer",
							"nullable": true,
							"description": "Gruppen | Modus"
						},
						"groups_value": {
							"nullable": false,
							"type": "array",
							"items": {
								"type": "string",
								"nullable": false,
							},
							"description": "Gruppen | Wert"
						},
						"expiry": {
							"nullable": true,
							"type": "intiger",
							"description": "Ablaufzeitpunkt"
						},
						/*
						"notification_target_url_template": {
							"type": "string",
							"nullable": true,
							"description": "Platz-Halter: id"
						},
						 */
					},
					"required": [
						"membership_number_mode",
						"membership_number_value",
						"name_mode",
						"name_value",
						"email_address_mode",
						"email_address_value",
						"groups_mode",
						"groups_value",
						"expiry",
					]
				}),
				"output_schema": () => ({
					"type": "object",
					"nullable": false,
					"properties": {
						"id": {
							"type": "number",
							"nullable": false,
						},
						"key": {
							"type": "string",
							"nullable": false,
						},
					},
					"additionalProperties": false,
					"required": [
						"id",
						"key",
					]
				}),
				"restriction": () => restriction_logged_in,
				"execution": () => async ({"input": input}) => {
					if (input === null) {
						return Promise.reject(new Error("impossible"));
					}
					else {
						if (
							(! _espe.conf.get().settings.misc.facultative_membership_number)
							&&
							(
								(input.membership_number_value === null)
								||
								(input.membership_number_value === "")
							)
						) {
							return Promise.resolve({
								"status_code": 400,
								"data": "membership number required"
							});
						}
						else {
							const invite_info : {id : _espe.type.invite_id; key : _espe.type.invite_key;} = await _espe.service.invite.create(
								{
									"membership_number_mode": _espe.helpers.invite_prefill_mode_decode(input.membership_number_mode),
									"membership_number_value": input.membership_number_value,
									"name_mode": _espe.helpers.invite_prefill_mode_decode(input.name_mode),
									"name_value": input.name_value,
									"email_address_mode": _espe.helpers.invite_prefill_mode_decode(input.email_address_mode),
									"email_address_value": input.email_address_value,
									"groups_mode": _espe.helpers.invite_prefill_mode_decode(input.groups_mode),
									"groups_value": input.groups_value,
								},
								{
									"expiry": input.expiry,
								}
							);
							return Promise.resolve({
								"status_code": 201,
								"data": invite_info
							});
						}
					}
				}
			}
		);
	}
	
}

