/*
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.database
{
	
	/**
	 */
	const _compatible_revisions : Array<string> = [
		"r4",
	];
	
		
	/**
	 */
	export function get_implementation(
	) : lib_plankton.database.type_database
	{
		switch (_espe.conf.get().database.kind) {
			case "sqlite": {
				type type_parameters = {
					path : string;
				};
				const parameters : type_parameters = (_espe.conf.get().database.data as type_parameters);
				return lib_plankton.database.sqlite_database(
					{
						"path": parameters.path,
						"verbose": false,
					}
				);
				break;
			}
			default: {
				throw (new Error("database implementation not available: " + _espe.conf.get().database.kind));
			}
		}
	}
	
	
	/**
	 */
	function get_revision(
	) : Promise<(null | string)>
	{
		return (
			get_implementation().query_select(
				{
					"source": "_meta",
					"fields": ["revision"],
				}
			)
			.then<(null | string)>(
				(rows) => Promise.resolve<(null | string)>(rows[0]["revision"])
			)
			.catch<(null | string)>(
				() => Promise.resolve<(null | string)>(null)
			)
		);
	}
	
	
	/**
	 */
	export async function check(
	) : Promise<void>
	{
		const revision : (null | string) = await get_revision();
		lib_plankton.log.info(
			"database_check",
			{
				"revision_found": revision,
				"revisions_compatible": _compatible_revisions,
			}
		);
		if (revision === null) {
			return Promise.reject<void>(new Error("database appearently missing"));
		}
		else {
			if (! _compatible_revisions.includes(revision)) {
				return Promise.reject<void>(new Error("database revision incompatible; found: " + revision + "; required: " + String(_compatible_revisions)));
			}
			else {
				return Promise.resolve<void>(undefined);
			}
		}
	}
	
}
