/*
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.conf
{
	
	/**
	 */
	const _schema : lib_plankton.conf.type_schema = {
		"nullable": false,
		"type": "object",
		"properties": {
			"version": {
				"nullable": false,
				"type": "integer",
				"enum": [1]
			},
			"log": {
				"nullable": false,
				"type": "array",
				"items": {
					"anyOf": [
						{
							"type": "object",
							"properties": {
								"kind": {
									"nullable": false,
									"type": "string",
									"enum": ["stdout"]
								},
								"data": {
									"nullable": false,
									"type": "object",
									"properties": {
										"threshold": {
											"nullable": false,
											"type": "string",
											"enum": [
												"debug",
												"info",
												"notice",
												"warning",
												"error"
											],
											"default": "info"
										},
										"format": {
											"nullable": false,
											"type": "string",
											"enum": [
												"human_readable",
												"jsonl",
												"jsonl_structured",
											],
											"default": "human_readable",
										},
									},
									"required": [
									]
								}
							}
						},
						{
							"type": "object",
							"properties": {
								"kind": {
									"nullable": false,
									"type": "string",
									"enum": ["file"]
								},
								"data": {
									"nullable": false,
									"type": "object",
									"properties": {
										"path": {
											"nullable": false,
											"type": "string"
										},
										"threshold": {
											"nullable": false,
											"type": "string",
											"enum": [
												"debug",
												"info",
												"notice",
												"warning",
												"error"
											],
											"default": "info"
										},
										"format": {
											"nullable": false,
											"type": "string",
											"enum": [
												"human_readable",
												"jsonl",
												"jsonl_structured",
											],
											"default": "human_readable",
										},
									},
									"required": [
									]
								}
							}
						},
					]
				},
				"default": [
					{
						"kind": "stdout",
						"data": {
							"threshold": "info"
						}
					}
				]
			},
			"server": {
				"nullable": false,
				"type": "object",
				"properties": {
					"address": {
						"nullable": false,
						"type": "string",
						"default": "::"
					},
					"port": {
						"nullable": false,
						"type": "integer",
						"default": 7845
					}
				},
				"required": [
				],
				"additionalProperties": false,
				"default": {
				}
			},
			"database": {
				"anyOf": [
					{
						"type": "object",
						"properties": {
							"kind": {
								"nullable": false,
								"type": "string",
								"enum": ["sqlite"]
							},
							"data": {
								"nullable": false,
								"type": "object",
								"properties": {
									"path": {
										"nullable": false,
										"type": "string",
										"default": "data.sqlite"
									}
								},
								"required": [
								],
								"default": {}
							}
						},
						"required": [
							"kind"
						]
					},
					{
						"type": "object",
						"properties": {
							"kind": {
								"nullable": false,
								"type": "string",
								"enum": ["postgresql"]
							},
							"data": {
								"nullable": false,
								"type": "object",
								"properties": {
									"host": {
										"nullable": false,
										"type": "string"
									},
									"port": {
										"nullable": false,
										"type": "integer",
										"default": 5432
									},
									"username": {
										"nullable": false,
										"type": "string"
									},
									"password": {
										"nullable": false,
										"type": "string"
									},
									"schema": {
										"nullable": false,
										"type": "string"
									}
								},
								"required": [
									"host",
									"username",
									"password",
									"scheme"
								]
							}
						},
						"required": [
							"kind",
							"data"
						]
					}
				],
				"default": {
					"kind": "sqlite"
				}
			},
			"session_management": {
				"nullable": false,
				"type": "object",
				"properties": {
					"in_memory": {
						"nullable": false,
						"type": "boolean",
						"default": true
					},
					"drop_all_at_start": {
						"nullable": false,
						"type": "boolean",
						"default": true
					},
					"lifetime": {
						"nullable": false,
						"type": "integer",
						"default": 3600,
					},
					"prolongation": {
						"nullable": true,
						"type": "integer",
						"default": 300,
					},
				},
				"required": [
				],
				"additionalProperties": false,
				"default": {}
			},
			"authentication": {
				"anyOf": [
					{
						"type": "object",
						"properties": {
							"kind": {
								"nullable": false,
								"type": "string",
								"enum": ["internal"]
							},
							"data": {
								"nullable": false,
								"type": "object",
								"properties": {
								},
								"required": [
								],
								"default": {}
							}
						},
						"required": [
							"kind"
						]
					},
					{
						"type": "object",
						"properties": {
							"kind": {
								"nullable": false,
								"type": "string",
								"enum": ["oidc"]
							},
							"data": {
								"nullable": false,
								"type": "object",
								"properties": {
									"url_authorization": {
										"nullable": false,
										"type": "string"
									},
									"url_token": {
										"nullable": false,
										"type": "string"
									},
									"url_userinfo": {
										"nullable": false,
										"type": "string"
									},
									"client_id": {
										"nullable": false,
										"type": "string"
									},
									"client_secret": {
										"nullable": false,
										"type": "string"
									},
									"backend_url_base": {
										"nullable": false,
										"type": "string"
									},
									"label": {
										"nullable": false,
										"type": "string",
										"default": "OIDC"
									}
								},
								"required": [
									"url_authorization",
									"url_token",
									"url_userinfo",
									"client_id",
									"client_secret",
									"backend_url_base"
								]
							}
						},
						"required": [
							"kind",
							"data"
						]
					}
				],
				"default": {
					"kind": "internal",
					"data": {
					}
				}
			},
			"external_resources": {
				"nullable": false,
				"type": "object",
				"properties": {
					"lifetime": {
						"nullable": false,
						"type": "integer",
						"default": 14400
					}
				},
				"additionalProperties": false,
				"required": [
				],
				"default": {
				}
			},
			"caldav": {
				"nullable": true,
				"type": "object",
				"properties": {
					"address": {
						"nullable": false,
						"type": "string"
					},
					"username": {
						"nullable": false,
						"type": "string"
					},
					"password": {
						"nullable": false,
						"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"
				],
				"additionalProperties": false,
				"default": null
			},
			"misc": {
				"nullable": false,
				"type": "object",
				"properties": {
					/**
					 * @todo make mandatory
					 */
					"auth_salt": {
						"nullable": false,
						"type": "string",
						"default": "unsafe_auth_salt"
					}
				},
				"required": [
				],
				"additionalProperties": false,
				"default": {}
			},
		},
		"required": [
			"version"
		],
		"additionalProperties": false
	};
	
	
	/**
	 */
	var _data : (null | any) = null;
	
	
	/**
	 */
	export function schema(
	) : lib_plankton.conf.type_schema
	{
		return _schema;
	}
	
	
	/**
	 */
	export function get(
	) : any
	{
		if (_data === null) {
			throw (new Error("conf not loaded yet"));
		}
		else {
			return _data;
		}
	}
	
	
	/**
	 */
	export async function init(
		path : string
	) : Promise<void>
	{
		_data = await lib_plankton.conf.load(
			_schema,
			path
		);
		return Promise.resolve<void>(undefined);
	}
	
}
