namespace _heimdall.state_repository
{
	
	/**
	 */
	function condition_decode(
		condition_encoded : int
	) : _heimdall.enum_condition
	{
		return {
			0: enum_condition.unknown,
			1: enum_condition.ok,
			2: enum_condition.concerning,
			3: enum_condition.critical,
		}[condition_encoded];
	}
	
	
	/**
	 */
	function condition_encode(
		condition : _heimdall.enum_condition
	) : int
	{
		switch (condition) {
			case enum_condition.unknown: return 0;
			case enum_condition.ok: return 1;
			case enum_condition.concerning: return 2;
			case enum_condition.critical: return 3;
		}
	}
	
	
	/**
	 */
	export async function setup(
		database_path : string
	) : Promise<void>
	{
		const result = await _heimdall.helpers.sqlite.query_set(
			database_path,
			{
				"template": "CREATE TABLE IF NOT EXISTS results(check_name TEXT NOT NULL, timestamp INTEGER NOT NULL, condition TEXT NOT NULL, notification_sent BOOLEAN NOT NULL, info TEXT NOT NULL);",
				"arguments": {
				},
			},
		);
	}
	
	
	/**
	 */
	export async function clean(
		database_path : string,
		time_to_live : int,
		erase_state : boolean
	) : Promise<int>
	{
		const result = await _heimdall.helpers.sqlite.query_put(
			database_path,
			{
				"template": "DELETE FROM results WHERE ((timestamp < :timestamp_min) OR :erase_state);",
				"arguments": {
					"timestamp_min": Math.floor((Date.now() / 1000) - time_to_live),
					"erase_state": erase_state,
				},
			}
		);
		return result.affected;
	}
	
	
	/**
	 */
	export async function probe(
		database_path : string,
		check_name : string,
		threshold : int
	) : Promise<
		Array<
			{
				timestamp : int;
				condition : _heimdall.enum_condition;
				notification_sent : boolean;
			}
		>
	>
	{
		const rows : Array<Array<any>> = await _heimdall.helpers.sqlite.query_get(
			database_path,
			{
				"template": "SELECT timestamp, condition, notification_sent FROM results WHERE (check_name = :check_name) ORDER BY timestamp DESC LIMIT :limit;",
				"arguments": {
					"check_name": check_name,
					"limit": threshold,
				},
			}
		);
		return (
			rows
			.map(
				row => ({
					"timestamp": row[0],
					"condition": condition_decode(row[1]),
					"notification_sent": row[2],
				})
			)
		);
	}
	
	
	/**
	 */
	export async function get_last_notification_timestamp(
		database_path : string,
		check_name : string
	) : Promise<(null | int)>
	{
		const rows : Array<Array<any>> = await _heimdall.helpers.sqlite.query_get(
			database_path,
			{
				"template": "SELECT MAX(timestamp) FROM results WHERE (check_name = :check_name) AND (notification_sent = TRUE);",
				"arguments": {
					"check_name": check_name,
				},
			}
		);
		return rows[0][0];
	}
	
	
	/**
	 */
	export async function feed(
		database_path : string,
		check_name : string,
		timestamp : int,
		condition : _heimdall.enum_condition,
		shall_send_notification : boolean,
		info : any
	) : Promise<void>
	{
		await _heimdall.helpers.sqlite.query_put(
			database_path,
			{
				"template": "INSERT INTO results(check_name, timestamp, condition, notification_sent, info) VALUES (:check_name, :timestamp, :condition, :notification_sent, :info);",
				"arguments": {
					"check_name": check_name,
					"timestamp": timestamp,
					"condition": condition_encode(condition),
					"notification_sent": shall_send_notification,
					"info": lib_plankton.json.encode(info),
				}
			}
		);
	}
	
}
