/*
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_events(
		rest_subject : lib_plankton.rest_http.type_rest
	) : void
	{
		register<
			null,
			(
				Array<
					{
						hash : _zeitbild.type_event_hash;
						calendar_id : int;
						calendar_name : string;
						hue : float;
						access_level : string;
						event_id : (null | int);
						event_object : _zeitbild.type_event_object;
					}
				>
				|
				string
			)
		>(
			rest_subject,
			lib_plankton.http.enum_method.get,
			"/events",
			{
				"description": "stellt Veranstaltungen aus verschiedenen Kalendern zusammen",
				"query_parameters": () => ([
					{
						"name": "from",
						"required": true,
						"description": "UNIX timestamp",
					},
					{
						"name": "to",
						"required": true,
						"description": "UNIX timestamp",
					},
					{
						"name": "calendar_ids",
						"required": false,
						"description": "comma separated",
					},
				]),
				"output_schema": () => ({
					"type": "array",
					"items": {
						"nullable": false,
						"type": "object",
						"additionalProperties": false,
						"properties": {
							"hash": {
								"nullable": false,
								"type": "string",
							},
							"calendar_id": {
								"nullable": false,
								"type": "number",
							},
							"calendar_name": {
								"nullable": false,
								"type": "string",
							},
							"hue": {
								"nullable": false,
								"type": "number"
							},
							"access_level": {
								"nullable": false,
								"type": "string",
								"enum": ["none","view","edit","admin"],
							},
							"event_id": {
								"nullable": true,
								"type": "number",
							},
							"event_object": {
								"nullable": false,
								"type": "object",
								"additionalProperties": false,
								"properties": {
									"name": {
										"type": "string",
										"nullable": false,
									},
									"begin": _zeitbild.api.datetime_type(
										{
											"nullable": false,
										}
									),
									"end": _zeitbild.api.datetime_type(
										{
											"nullable": true,
										}
									),
									"location": {
										"type": "string",
										"nullable": true,
									},
									"link": {
										"type": "string",
										"nullable": true,
									},
									"description": {
										"type": "string",
										"nullable": true,
									},
								},
								"required": [
									"name",
									"begin",
									"end",
									"location",
									"description",
								]
							}
						},
						"required": [
							"calendar_id",
							"event_id",
							"calendar_name",
							"event_object",
						],
					}
				}),
				"restriction": restriction_none,
				"execution": async (stuff) => {
					const user_id : (null | _zeitbild.type_user_id) = await (
						session_from_stuff(stuff)
						.then(
							(session : {key : string; value : lib_plankton.session.type_session;}) => (
								_zeitbild.service.user.identify(session.value.name)
								.catch(x => Promise.resolve(null))
							)
						)
						.catch(x => Promise.resolve(null))
					);
					
					const from : lib_plankton.pit.type_pit = parseInt(stuff.query_parameters["from"]);
					const to : lib_plankton.pit.type_pit = parseInt(stuff.query_parameters["to"]);
					const calendar_ids_wanted : (null | Array<_zeitbild.type_calendar_id>) = (
						(
							("calendar_ids" in stuff.query_parameters)
							&&
							(stuff.query_parameters["calendar_ids"] !== null)
						)
						?
						lib_plankton.call.convey(
							stuff.query_parameters["calendar_ids"],
							[
								(x : string) => x.split(","),
								(x : Array<string>) => x.map(parseInt),
								(x : Array<int>) => x.filter(y => (! isNaN(y)))
							]
						)
						:
						null
					);
					return (
						_zeitbild.service.calendar.gather_events(
							calendar_ids_wanted,
							from,
							to,
							user_id
						)
						.then(
							(data) => Promise.resolve(
								{
									"status_code": 200,
									"data": (
										data
										.map(
											(entry) => ({
												// todo
												"hash": entry.hash,
												"calendar_id": entry.calendar_id,
												"calendar_name": entry.calendar_name,
												"hue": entry.hue,
												"access_level": _zeitbild.api.access_level_encode(entry.access_level),
												"event_id": entry.event_id,
												"event_object": entry.event_object,
											})
										)
									),
								}
							)
						)
						.catch(
							(reason) => Promise.resolve(
								{
									"status_code": 403,
									"data": String(reason),
								}
							)
						)
					);
				}
			}
		);
	}
	
}
