/*
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_examine(
		rest_subject : lib_plankton.rest_http.type_rest
	) : void
	{
		lib_plankton.rest_http.register<
			any,
			any
		>(
			rest_subject,
			lib_plankton.http.enum_method.get,
			_espe.api.full_path("/invite/examine"),
			{
				/**
				 * @todo translation
				 */
				"description": () => "gibt die Daten einer Einladung anhand ihres Schlüssels aus",
				"query_parameters": () => [
					{
						"name": "key",
						"required": true,
						"description": "key",
					}
				],
				"output_schema": () => ({
					"type": "object",
					"nullable": false,
					"additionalProperties": false,
					"properties": {
						"expiry": {
							"nullable": true,
							"type": "intiger",
							"description": "Ablaufzeitpunkt"
						},
						"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_mode": {
							"type": "integer",
							"nullable": true,
							"description": "Gruppen | Modus"
						},
						"groups_value": {
							"nullable": false,
							"type": "array",
							"items": {
								"type": "string",
								"nullable": false,
							},
							"description": "Gruppen | Wert"
						},
					},
					"required": [
						"expiry",
						"membership_number_mode",
						"membership_number_value",
						"name_mode",
						"name_value",
						"email_address_mode",
						"email_address_value",
						"groups_mode",
						"groups_value",
					]
				}),
				"restriction": () => restriction_none,
				"execution": () => ({"query_parameters": query_parameters, "input": input}) => {
					const invite_key : _espe.type.invite_key = query_parameters["key"];
					return (
						_espe.service.invite.examine(invite_key)
						.then(
							(invite_object) => Promise.resolve({
								"status_code": 200,
								"data": {
									"expiry": invite_object.expiry,
									"membership_number_mode": _espe.helpers.invite_prefill_mode_encode(invite_object.membership_number_mode),
									"membership_number_value": invite_object.membership_number_value,
									"name_mode": _espe.helpers.invite_prefill_mode_encode(invite_object.name_mode),
									"name_value": invite_object.name_value,
									"email_address_mode": _espe.helpers.invite_prefill_mode_encode(invite_object.email_address_mode),
									"email_address_value": invite_object.email_address_value,
									"groups_mode": _espe.helpers.invite_prefill_mode_encode(invite_object.groups_mode),
									"groups_value": invite_object.groups_value,
								}
							})
						)
						.catch(
							(error) => Promise.resolve({
								"status_code": 404,
								"data": "not found"
							})
						)
					);
				}
			}
		);
	}
	
}
