/*
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_read(
		rest_subject : lib_plankton.rest.type_rest
	) : void
	{
		register<
			null,
			{
				membership_number : (null | string);
				name_real_value : string;
				name_real_index : int;
				email_address_private : (null | string);
				registered : boolean;
				enabled : boolean;
				email_use_veiled_address : boolean;
				email_use_nominal_address : boolean;
				email_redirect_to_private_address : boolean;
				email_allow_sending : boolean;
				password_set : boolean;
				email_address_veiled : (null | string);
				email_address_nominal : string;
				name_login : string;
			}
		>(
			rest_subject,
			lib_plankton.http.enum_method.get,
			"/member/read/:id",
			{
				"description": "gibt ein Mitglied anhand seiner ID aus",
				"output_schema": () => ({
					"nullable": false,
					"type": "object",
					"properties": {
						"membership_number": {
							"nullable": true,
							"type": "string"
						},
						"name_real_value": {
							"nullable": false,
							"type": "string"
						},
						"name_real_index": {
							"nullable": false,
							"type": "number"
						},
						"email_address_private": {
							"nullable": true,
							"type": "string"
						},
						"registered": {
							"nullable": false,
							"type": "boolean"
						},
						"enabled": {
							"nullable": false,
							"type": "boolean"
						},
						"email_use_veiled_address": {
							"nullable": false,
							"type": "boolean"
						},
						"email_use_nominal_address": {
							"nullable": false,
							"type": "boolean"
						},
						"email_redirect_to_private_address": {
							"nullable": false,
							"type": "boolean"
						},
						"email_allow_sending": {
							"nullable": false,
							"type": "boolean"
						},
						"password_set": {
							"nullable": false,
							"type": "boolean"
						},
						"email_address_veiled": {
							"nullable": true,
							"type": "string"
						},
						"email_address_nominal": {
							"nullable": false,
							"type": "string"
						},
						"name_login": {
							"nullable": false,
							"type": "string"
						},
					},
					"additionalProperties": false,
					"required": [
						"membership_number",
						"name_real_value",
						"name_real_index",
						"email_address_private",
						"registered",
						"enabled",
						"email_use_veiled_address",
						"email_use_nominal_address",
						"email_redirect_to_private_address",
						"email_allow_sending",
						"password_set",
						"email_address_veiled",
						"email_address_nominal",
						"name_login",
					]
				}),
				"restriction": restriction_logged_in,
				"execution": async ({"path_parameters": path_parameters, "input": input}) => {
					const member_id : _espe.type.member_id = parseInt(path_parameters["id"]);
					const member_object : _espe.type.member_object = await _espe.service.member.get(member_id);
					return Promise.resolve({
						"status_code": 200,
						"data": {
							"membership_number": member_object.membership_number,
							"name_real_value": member_object.name_real_value,
							"name_real_index": member_object.name_real_index,
							"email_address_private": member_object.email_address_private,
							"registered": member_object.registered,
							"enabled": member_object.enabled,
							"email_use_veiled_address": member_object.email_use_veiled_address,
							"email_use_nominal_address": member_object.email_use_nominal_address,
							"email_redirect_to_private_address": member_object.email_redirect_to_private_address,
							"email_allow_sending": member_object.email_allow_sending,
							"password_set": (member_object.password_image !== null),
							"name_login": _espe.service.member.name_login(member_object),
							"email_address_veiled": _espe.service.member.email_address_veiled(member_object),
							"email_address_nominal": _espe.service.member.email_address_nominal(member_object),
						},
					});
				}
			}
		);
	}
	
}
