/*
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_member_project(
		rest_subject : lib_plankton.rest.type_rest
	) : void
	{
		register<
			{
				membership_number : (null | string);
				name_real_value : string;
				email_address_private : (null | string);
			},
			(
				string
				|
				_espe.type.member_id
			)
		>(
			rest_subject,
			lib_plankton.http.enum_method.post,
			"/member/project",
			{
				"description": "erstellt ein neues Mitglied und gibt die erzeugte ID aus",
				"input_schema": () => ({
					"type": "object",
					"nullable": false,
					"additionalProperties": false,
					"properties": {
						"membership_number": {
							"type": "string",
							"nullable": false,
							"description": "Mitgliedsnummer"
						},
						"name_real_value": {
							"type": "string",
							"nullable": false,
							"description": "Klarname"
						},
						"email_address_private": {
							"type": "string",
							"nullable": true,
							"description": "private E-Mail-Adresse"
						},
					},
					"required": [
						"membership_number",
						"name_real_value",
					]
				}),
				"output_schema": () => ({
					"type": "number",
					"nullable": false,
				}),
				"restriction": restriction_logged_in,
				"execution": async ({"input": input}) => {
					if (input === null) {
						return Promise.reject(new Error("impossible"));
					}
					else {
						if (
							(! _espe.conf.get().settings.facultative_membership_number)
							&&
							(
								(input.membership_number === null)
								||
								(input.membership_number === "")
							)
						) {
							return Promise.resolve({
								"status_code": 400,
								"data": "membership number required"
							});
						}
						else {
							const member_id : _espe.type.member_id = await _espe.service.member.project(
								{
									"membership_number": input.membership_number,
									"name_real_value": input.name_real_value,
									"email_address_private": (
										("email_address_private" in input)
										? (
											(input.email_address_private !== "")
											? input.email_address_private
											: null
										)
										: null
									),
								}
							);
							return Promise.resolve({
								"status_code": 201,
								"data": member_id
							});
						}
					}
				}
			}
		);
	}
	
}
