/*
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
{
	
	/**
	 * @todo zeitliche Begrenzung?
	 */
	export function register_member_register(
		rest_subject : lib_plankton.rest.type_rest
	) : void
	{
		register<
			{
				email_use_veiled_address : boolean;
				email_use_nominal_address : boolean;
				email_redirect_to_private_address : boolean;
				password : (null | string);
				notification_target_url_template ?: (null | string);
			},
			Array<
				{
					incident : string;
					details : Record<string, any>;
				}
			>
		>(
			rest_subject,
			lib_plankton.http.enum_method.post,
			"/member/register/:id",
			{
				"description": "nimmt zusätzliche Angaben eines Mitglieds entgegen",
				"input_schema": () => ({
					"type": "object",
					"nullable": false,
					"additionalProperties": false,
					"properties": {
						"email_use_veiled_address": {
							"type": "boolean",
							"nullable": false,
							"description": "ob die nummern-basierte E-Mail-Adresse eingerichtet werden soll",
						},
						"email_use_nominal_address": {
							"type": "boolean",
							"nullable": false,
							"description": "ob die namens-basierte E-Mail-Adresse eingerichtet werden soll",
						},
						"email_redirect_to_private_address": {
							"type": "boolean",
							"nullable": false,
							"description": "ob auf die Partei-Adressen eingehende E-Mails zur privaten Adresse weitergeleitet werden sollen",
						},
						"password": {
							"type": "string",
							"nullable": true,
							"description": "Passwort für alle Netz-Dienste",
						},
						"notification_target_url_template": {
							"type": "string",
							"nullable": true,
							"description": "Platz-Halter: id"
						},
					},
					"required": [
						"email_use_veiled_address",
						"email_use_nominal_address",
						"email_redirect_to_private_address",
						"password",
					]
				}),
				"output_schema": () => ({
					"nullable": false,
					"type": "array",
					"items": {
						"nullable": false,
						"type": "object",
						"properties": {
							"incident": {
								"nullable": false,
								"type": "string"
							},
							"details": {
								"nullable": false,
								"type": "object",
								"properties": {},
								"additionalProperties": {
									"nullable": true
								},
								"required": []
							},
						},
						"additionalProperties": false,
						"required": [
							"incident",
							"details",
						]
					}
				}),
				"query_parameters": [
					{
						"name": "key",
						"required": true,
						"description": "Zugriffs-Schlüssel",
					},
				],
				"restriction": restriction_verification(
					stuff => parseInt(stuff.path_parameters["id"]),
					stuff => stuff.query_parameters["key"]
				),
				"execution": ({"path_parameters": path_parameters, "input": input}) => {
					if (input === null) {
						return Promise.reject(new Error("impossible"));
					}
					else {
						const member_id : _espe.type.member_id = parseInt(path_parameters["id"]);
						return (
							_espe.service.member.register(
								member_id,
								{
									"email_use_veiled_address": input.email_use_veiled_address,
									"email_use_nominal_address": input.email_use_nominal_address,
									"email_redirect_to_private_address": input.email_redirect_to_private_address,
									"password": input.password,
								},
								{
									"notification_target_url_template": (input.notification_target_url_template ?? null),
								}
							)
							.then(
								flaws => Promise.resolve({
									"status_code": (
										(flaws.length <= 0)
										? 200
										: 409
									),
									"data": flaws
								})
							)
						);
					}
				},
			}
		)
	}
	
}
