/*
This file is part of »zeitbild«.

Copyright 2025 'kcf' <fenris@folksprak.org>

»zeitbild« is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

»zeitbild« 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with »zeitbild«. If not, see <http://www.gnu.org/licenses/>.
 */


namespace _zeitbild.api
{
	
	/**
	 */
	export function register_user_dav_conf(
		rest_subject : lib_plankton.rest_http.type_rest
	) : void
	{
		register<
			null,
			(
				null
				|
				{
					address : string;
					username : string;
					password : (null | string);
					setup_hints : Array<
						{
							label : string;
							link : string;
							remark : (null | string);
						}
					>;
				}
			)
		>(
			rest_subject,
			lib_plankton.http.enum_method.get,
			"/user_dav_conf",
			{
				"description": "gibt die CalDAV-Zugangsdaten eines Nutzers aus",
				"output_schema": () => ({
					"nullable": true,
					"type": "object",
					"properties": {
						"address": {
							"nullable": false,
							"type": "string"
						},
						"username": {
							"nullable": false,
							"type": "string"
						},
						"password": {
							"nullable": true,
							"type": "string"
						},
						"setup_hints": {
							"nullable": false,
							"type": "array",
							"items": {
								"nullable": false,
								"type": "object",
								"properties": {
									"label": {
										"nullable": false,
										"type": "string"
									},
									"link": {
										"nullable": false,
										"type": "string"
									},
									"remark": {
										"nullable": true,
										"type": "string",
										"default": null,
									},
								},
								"required": [
									"label",
									"link",
								],
								"additionalProperties": false
							},
							"default": []
						},
					},
					"required": [
						"address",
						"username",
						"password",
						"setup_hints",
					],
					"additionalProperties": false
				}),
				"restriction": restriction_logged_in,
				"execution": async (stuff) => {
					let result : (
						null
						|
						{
							address : string;
							username : string;
							password : (null | string);
							setup_hints : Array<
								{
									label : string;
									link : string;
									remark : (null | string);
								}
							>;
						}
					) = null;
					const raw : (null | any) = _zeitbild.conf.get()["caldav"];
					if (raw === null)
					{
						result = null;
					}
					else
					{
						const session : {key : string; value : lib_plankton.session.type_session;} = await session_from_stuff(stuff);
						const user_id : _zeitbild.type_user_id = await _zeitbild.service.user.identify(session.value.name);
						const user_object : _zeitbild.type_user_object = await _zeitbild.service.user.get(user_id);
						const arguments_ : Record<string, string> = Object.fromEntries(
							[
								{"key": "username", "value": user_object.name},
								{"key": "password", "value": user_object.dav_token},
							]
							.filter(
								entry => (entry.value !== null)
							)
							.map(
								entry => ([entry.key, entry.value as string])
							)
						);
						result = {
							"address": lib_plankton.string.coin(raw["address"], arguments_),
							"username": lib_plankton.string.coin(raw["username"], arguments_),
							"password": (
								(user_object.dav_token === null)
								?
								null
								:
								lib_plankton.string.coin(raw["password"], arguments_)
							),
							"setup_hints": raw["setup_hints"],
						};
					}
					return Promise.resolve(
						{
							"status_code": 200,
							"data": result,
						}
					);
				}
			}
		);
	}
	
}
