/*
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 ausgeklügelte Durchsatzratenbegrenzung
	 */
	export function register_member_password_change_execute(
		rest_subject : lib_plankton.rest.type_rest
	) : void
	{
		register<
			{
				token : string;
				password_new : string;
			},
			Array<
				{
					incident : string;
					details : Record<string, any>;
				}
			>
		>(
			rest_subject,
			lib_plankton.http.enum_method.patch,
			"/member/password_change/execute/:id",
			{
				"description": "Führt eine Passwort-Änderung für ein Mitglied durch",
				"input_schema": () => ({
					"nullable": false,
					"type": "object",
					"properties": {
						"token": {
							"nullable": false,
							"type": "string"
						},
						"password_new": {
							"nullable": false,
							"type": "string",
							"description": "das neue Passwort"
						},
					},
					"additionalProperties": false,
					"required": [
						"token",
						"password_new",
					]
				}),
				"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",
						]
					}
				}),
				"restriction": restriction_none,
				"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.password_change_execute(
								member_id,
								input.token,
								input.password_new
							)
							.then(
								flaws => Promise.resolve({
									"status_code": (
										(flaws.length <= 0)
										? 200
										: 409
									),
									"data": flaws
								})
							)
						);
					}
				},
			}
		)
	}
	
}
