/* database.c generated by valac 0.56.19, the Vala compiler
 * generated from database.vala, do not modify */

/*
 * Copyright (c) 2022-2025 focus-timer contributors
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <gom/gom.h>
#include "ft_core.h"
#include <gio/gio.h>
#include <sqlite3.h>
#include <glib-object.h>
#include <config.h>

#define FT_DATABASE_VERSION ((guint) 3)
#define FT_DATABASE_MIGRATIONS_URI "resource:///io/github/focustimerhq/FocusTimer/migrations"
#define FT_DATABASE_DATE_FORMAT "%Y-%m-%d"
#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block11Data Block11Data;
#define _g_async_queue_unref0(var) ((var == NULL) ? NULL : (var = (g_async_queue_unref (var), NULL)))
#define _sqlite3_finalize0(var) ((var == NULL) ? NULL : (var = (sqlite3_finalize (var), NULL)))
#define _g_date_time_unref0(var) ((var == NULL) ? NULL : (var = (g_date_time_unref (var), NULL)))
typedef struct _Block12Data Block12Data;
#define _sqlite3_close0(var) ((var == NULL) ? NULL : (var = (sqlite3_close (var), NULL)))
#define _sqlite3_backup_finish0(var) ((var == NULL) ? NULL : (var = (sqlite3_backup_finish (var), NULL)))

struct _Block11Data {
	int _ref_count_;
	GAsyncQueue* result_queue;
	GomAdapter* adapter;
};

struct _Block12Data {
	int _ref_count_;
	gchar* backup_path;
};

VALA_EXTERN GomAdapter* ft_database_adapter;
GomAdapter* ft_database_adapter = NULL;
VALA_EXTERN GomRepository* ft_database_repository;
GomRepository* ft_database_repository = NULL;

VALA_EXTERN void ft_database_make_directory_with_parents (GFile* directory);
VALA_EXTERN void ft_database_execute_sql (GomAdapter* adapter,
                              const gchar* sql,
                              GError** error);
VALA_EXTERN gboolean ft_database_is_migration_applied (GomRepository* repository,
                                           GomAdapter* adapter,
                                           guint version);
VALA_EXTERN gboolean ft_is_test (void);
VALA_EXTERN gboolean ft_database_check_health (GomAdapter* adapter);
static Block11Data* block11_data_ref (Block11Data* _data11_);
static void block11_data_unref (void * _userdata_);
static void _g_free0_ (gpointer var);
static void __lambda83_ (Block11Data* _data11_);
static gboolean* _bool_dup (gboolean* self);
static void ___lambda83__gom_adapter_callback (GomAdapter* adapter,
                                        gpointer self);
VALA_EXTERN void ft_database_rename_corrupted_file (GFile* database_file,
                                        GError** error);
VALA_EXTERN void ft_database_restore_database_file (GFile* database_file,
                                        GError** error);
VALA_EXTERN gchar* ft_database_build_database_path (void);
VALA_EXTERN gchar* ft_database_build_backup_path (void);
VALA_EXTERN void ft_database_delete_file_if_exists (const gchar* path);
VALA_EXTERN void ft_database_create_backup (const gchar* backup_path);
static Block12Data* block12_data_ref (Block12Data* _data12_);
static void block12_data_unref (void * _userdata_);
static void __lambda84_ (Block12Data* _data12_,
                  GomAdapter* adapter);
static void ___lambda84__gom_adapter_callback (GomAdapter* adapter,
                                        gpointer self);
VALA_EXTERN gboolean ft_database_should_create_backup (const gchar* backup_path);
static gboolean __lambda85_ (void);
static gboolean ___lambda85__gsource_func (gpointer self);
VALA_EXTERN void ft_database_open_repository (GFile* database_file,
                                  GomAdapter** adapter,
                                  GomRepository** repository);
static void __lambda86_ (GomAdapter* _adapter);
static void ___lambda86__gom_adapter_callback (GomAdapter* adapter,
                                        gpointer self);
static gboolean _ft_database_migrate_repository_gom_repository_migrator (GomRepository* repository,
                                                                  GomAdapter* adapter,
                                                                  guint version,
                                                                  gpointer self,
                                                                  GError** error);
VALA_EXTERN void ft_database_close_repository (GomRepository* repository);
VALA_EXTERN gchar* ft_database_chug_zeros (const gchar* str);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);
static gssize _vala_array_length (gpointer array);

GomRepository*
ft_database_get_repository (void)
{
	GomRepository* _tmp0_;
	GomRepository* result;
	_tmp0_ = ft_database_repository;
	result = _tmp0_;
	return result;
}

void
ft_database_make_directory_with_parents (GFile* directory)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (directory != NULL);
	if (!g_file_query_exists (directory, NULL)) {
		{
			g_file_make_directory_with_parents (directory, NULL, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch0_g_error;
			}
		}
		goto __finally0;
		__catch0_g_error:
		{
			GError* _error_ = NULL;
			GError* _tmp0_;
			const gchar* _tmp1_;
			_error_ = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp0_ = _error_;
			_tmp1_ = _tmp0_->message;
			g_warning ("database.vala:32: Failed to create directory: %s", _tmp1_);
			_g_error_free0 (_error_);
		}
		__finally0:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
	}
}

/**
     * Convenience function to execute multiline SQL.
     *
     * `Gom.Adapter.execute_sql` is limited to single line queries.
     *
     * This MUST be called from within a write transaction using
     * Gom.Adapter.queue_write().
     */
static gint
_sqlite3_exec (sqlite3* self,
               const gchar* sql,
               sqlite3_callback callback,
               gpointer callback_target,
               gchar** errmsg)
{
	gchar* _vala_errmsg = NULL;
	const gchar* sqlite_errmsg = NULL;
	gint ec = 0;
	const gchar* _tmp0_ = NULL;
	gint _tmp1_;
	const gchar* _tmp4_;
	gint result;
	g_return_val_if_fail (self != NULL, -1);
	g_return_val_if_fail (sql != NULL, -1);
	_tmp1_ = sqlite3_exec (self, sql, callback, callback_target, (char**) (&_tmp0_));
	sqlite_errmsg = _tmp0_;
	ec = _tmp1_;
	if ((&_vala_errmsg) != NULL) {
		const gchar* _tmp2_;
		gchar* _tmp3_;
		_tmp2_ = sqlite_errmsg;
		_tmp3_ = g_strdup (_tmp2_);
		_g_free0 (_vala_errmsg);
		_vala_errmsg = _tmp3_;
	} else {
		_g_free0 (_vala_errmsg);
		_vala_errmsg = NULL;
	}
	_tmp4_ = sqlite_errmsg;
	sqlite3_free ((void*) _tmp4_);
	result = ec;
	if (errmsg) {
		*errmsg = _vala_errmsg;
	} else {
		_g_free0 (_vala_errmsg);
	}
	return result;
}

void
ft_database_execute_sql (GomAdapter* adapter,
                         const gchar* sql,
                         GError** error)
{
	sqlite3* database = NULL;
	sqlite3* _tmp0_;
	gchar* error_message = NULL;
	sqlite3* _tmp1_;
	gchar* _tmp2_ = NULL;
	gint _tmp3_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (adapter != NULL);
	g_return_if_fail (sql != NULL);
	_tmp0_ = gom_adapter_get_handle (adapter);
	database = _tmp0_;
	_tmp1_ = database;
	_tmp3_ = _sqlite3_exec (_tmp1_, sql, NULL, NULL, &_tmp2_);
	_g_free0 (error_message);
	error_message = _tmp2_;
	if (_tmp3_ != SQLITE_OK) {
		const gchar* _tmp4_;
		GError* _tmp5_;
		_tmp4_ = error_message;
		_tmp5_ = g_error_new_literal (GOM_ERROR, GOM_ERROR_COMMAND_SQLITE, _tmp4_);
		_inner_error0_ = _tmp5_;
		g_propagate_error (error, _inner_error0_);
		_g_free0 (error_message);
		return;
	}
	_g_free0 (error_message);
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

gboolean
ft_database_is_migration_applied (GomRepository* repository,
                                  GomAdapter* adapter,
                                  guint version)
{
	GError* _inner_error0_ = NULL;
	gboolean result;
	g_return_val_if_fail (repository != NULL, FALSE);
	g_return_val_if_fail (adapter != NULL, FALSE);
	{
		GomCursor* cursor = NULL;
		GomCommand* check_command = NULL;
		GObject* _tmp0_;
		GObject* _tmp1_;
		GomCommand* _tmp2_;
		GomCommand* _tmp3_;
		GomCommand* _tmp4_;
		GomCursor* _tmp5_ = NULL;
		GomCursor* _tmp6_;
		gboolean _tmp7_ = FALSE;
		GomCursor* _tmp8_;
		cursor = NULL;
		_tmp0_ = g_object_new (gom_command_get_type (), "adapter", adapter, NULL);
		_tmp1_ = G_IS_INITIALLY_UNOWNED (_tmp0_) ? g_object_ref_sink (_tmp0_) : _tmp0_;
		check_command = G_TYPE_CHECK_INSTANCE_CAST (_tmp1_, gom_command_get_type (), GomCommand);
		_tmp2_ = check_command;
		gom_command_set_sql (_tmp2_, "SELECT 1 FROM _gom_version WHERE version = ? LIMIT 1;");
		_tmp3_ = check_command;
		gom_command_set_param_uint (_tmp3_, 0U, version);
		_tmp4_ = check_command;
		gom_command_execute (_tmp4_, &_tmp5_, &_inner_error0_);
		_g_object_unref0 (cursor);
		_tmp6_ = _g_object_ref0 (_tmp5_);
		cursor = _tmp6_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (check_command);
			_g_object_unref0 (cursor);
			goto __catch0_g_error;
		}
		_tmp8_ = cursor;
		if (_tmp8_ != NULL) {
			GomCursor* _tmp9_;
			_tmp9_ = cursor;
			_tmp7_ = gom_cursor_next (_tmp9_);
		} else {
			_tmp7_ = FALSE;
		}
		if (_tmp7_) {
			result = TRUE;
			_g_object_unref0 (check_command);
			_g_object_unref0 (cursor);
			return result;
		}
		_g_object_unref0 (check_command);
		_g_object_unref0 (cursor);
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_inner_error0_);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gboolean _tmp10_ = FALSE;
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return _tmp10_;
	}
	result = FALSE;
	return result;
}

static const gchar*
string_to_string (const gchar* self)
{
	const gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	result = self;
	return result;
}

static gpointer
_g_error_copy0 (gpointer self)
{
	return self ? g_error_copy (self) : NULL;
}

gboolean
ft_database_migrate_repository (GomRepository* repository,
                                GomAdapter* adapter,
                                guint version,
                                GError** error)
{
	gboolean is_test = FALSE;
	guint8* file_contents = NULL;
	gint file_contents_length1 = 0;
	gint _file_contents_size_ = 0;
	GFile* file = NULL;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	GFile* _tmp5_;
	GFile* _tmp6_;
	GFile* _tmp7_;
	guint8* _tmp8_ = NULL;
	gsize _tmp9_ = 0;
	GError* _inner_error0_ = NULL;
	gboolean result;
	g_return_val_if_fail (repository != NULL, FALSE);
	g_return_val_if_fail (adapter != NULL, FALSE);
	is_test = ft_is_test ();
	if (ft_database_is_migration_applied (repository, adapter, version)) {
		if (!is_test) {
			g_info ("database.vala:92: Migration version %u already applied, skipping", version);
		}
		result = TRUE;
		return result;
	}
	_tmp0_ = string_to_string (FT_DATABASE_MIGRATIONS_URI);
	_tmp1_ = g_strdup_printf ("%u", version);
	_tmp2_ = _tmp1_;
	_tmp3_ = g_strconcat (_tmp0_, "/version-", _tmp2_, ".sql", NULL);
	_tmp4_ = _tmp3_;
	_tmp5_ = g_file_new_for_uri (_tmp4_);
	_tmp6_ = _tmp5_;
	_g_free0 (_tmp4_);
	_g_free0 (_tmp2_);
	file = _tmp6_;
	_tmp7_ = file;
	g_file_load_contents (_tmp7_, NULL, &_tmp8_, &_tmp9_, NULL, &_inner_error0_);
	file_contents = (g_free (file_contents), NULL);
	file_contents = _tmp8_;
	file_contents_length1 = _tmp9_;
	_file_contents_size_ = file_contents_length1;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gboolean _tmp10_ = FALSE;
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (file);
		file_contents = (g_free (file_contents), NULL);
		return _tmp10_;
	}
	{
		guint8* _tmp11_;
		gint _tmp11__length1;
		if (!is_test) {
			g_info ("database.vala:105: Migrating database to version %u", version);
		}
		_tmp11_ = file_contents;
		_tmp11__length1 = file_contents_length1;
		ft_database_execute_sql (adapter, (const gchar*) _tmp11_, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* _error_ = NULL;
		GError* _tmp12_;
		GError* _tmp13_;
		_error_ = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp12_ = _error_;
		_tmp13_ = _g_error_copy0 (_tmp12_);
		_inner_error0_ = _tmp13_;
		_g_error_free0 (_error_);
		goto __finally0;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gboolean _tmp14_ = FALSE;
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (file);
		file_contents = (g_free (file_contents), NULL);
		return _tmp14_;
	}
	result = TRUE;
	_g_object_unref0 (file);
	file_contents = (g_free (file_contents), NULL);
	return result;
}

/**
     * Check database health using PRAGMA `quick_check`.
     *
     * This function internally uses queue_read to run the check in the
     * GOM worker thread, then blocks the calling thread until the result
     * is available (similar to how `open_sync` / `close_sync` work).
     *
     * `quick_check` is faster than `integrity_check`. Should be good enough for most cases.
     */
static Block11Data*
block11_data_ref (Block11Data* _data11_)
{
	g_atomic_int_inc (&_data11_->_ref_count_);
	return _data11_;
}

static void
block11_data_unref (void * _userdata_)
{
	Block11Data* _data11_;
	_data11_ = (Block11Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data11_->_ref_count_)) {
		_g_async_queue_unref0 (_data11_->result_queue);
		_g_object_unref0 (_data11_->adapter);
		g_slice_free (Block11Data, _data11_);
	}
}

static void
_g_free0_ (gpointer var)
{
	(var == NULL) ? NULL : (var = (g_free (var), NULL));
}

static gboolean*
_bool_dup (gboolean* self)
{
	gboolean* dup;
	dup = g_new0 (gboolean, 1);
	memcpy (dup, self, sizeof (gboolean));
	return dup;
}

static gpointer
__bool_dup0 (gpointer self)
{
	return self ? _bool_dup (self) : NULL;
}

static void
__lambda83_ (Block11Data* _data11_)
{
	gboolean is_healthy = FALSE;
	sqlite3* db = NULL;
	sqlite3* _tmp0_;
	sqlite3_stmt* statement = NULL;
	sqlite3* _tmp1_;
	sqlite3_stmt* _tmp2_ = NULL;
	gint _tmp3_;
	gboolean* _tmp11_;
	is_healthy = TRUE;
	_tmp0_ = gom_adapter_get_handle (_data11_->adapter);
	db = _tmp0_;
	_tmp1_ = db;
	_tmp3_ = sqlite3_prepare_v2 (_tmp1_, "PRAGMA quick_check;", -1, &_tmp2_, NULL);
	_sqlite3_finalize0 (statement);
	statement = _tmp2_;
	if (_tmp3_ != SQLITE_OK) {
		sqlite3* _tmp4_;
		const gchar* _tmp5_;
		_tmp4_ = db;
		_tmp5_ = sqlite3_errmsg (_tmp4_);
		g_warning ("database.vala:137: Failed to prepare database health check: %s", _tmp5_);
	} else {
		sqlite3_stmt* _tmp6_;
		_tmp6_ = statement;
		if (sqlite3_step (_tmp6_) != SQLITE_ROW) {
			g_warning ("database.vala:140: Database health check returned no results.");
		} else {
			const gchar* _result_ = NULL;
			sqlite3_stmt* _tmp7_;
			const gchar* _tmp8_;
			const gchar* _tmp9_;
			_tmp7_ = statement;
			_tmp8_ = (const gchar*) sqlite3_column_text (_tmp7_, 0);
			_result_ = _tmp8_;
			_tmp9_ = _result_;
			if (g_strcmp0 (_tmp9_, "ok") == 0) {
				is_healthy = TRUE;
			} else {
				const gchar* _tmp10_;
				_tmp10_ = _result_;
				g_warning ("database.vala:148: Database health check failed: %s", _tmp10_);
				is_healthy = FALSE;
			}
		}
	}
	_tmp11_ = __bool_dup0 (&is_healthy);
	g_async_queue_push (_data11_->result_queue, _tmp11_);
	_sqlite3_finalize0 (statement);
}

static void
___lambda83__gom_adapter_callback (GomAdapter* adapter,
                                   gpointer self)
{
	__lambda83_ (self);
	block11_data_unref (self);
}

gboolean
ft_database_check_health (GomAdapter* adapter)
{
	Block11Data* _data11_;
	GomAdapter* _tmp0_;
	GAsyncQueue* _tmp1_;
	gpointer _tmp2_;
	gboolean* _tmp3_;
	gboolean _tmp4_;
	gboolean result;
	g_return_val_if_fail (adapter != NULL, FALSE);
	_data11_ = g_slice_new0 (Block11Data);
	_data11_->_ref_count_ = 1;
	_tmp0_ = _g_object_ref0 (adapter);
	_g_object_unref0 (_data11_->adapter);
	_data11_->adapter = _tmp0_;
	_tmp1_ = g_async_queue_new_full (_g_free0_);
	_data11_->result_queue = _tmp1_;
	gom_adapter_queue_read (_data11_->adapter, ___lambda83__gom_adapter_callback, block11_data_ref (_data11_));
	_tmp2_ = g_async_queue_pop (_data11_->result_queue);
	_tmp3_ = (gboolean*) _tmp2_;
	_tmp4_ = *_tmp3_;
	_g_free0 (_tmp3_);
	result = _tmp4_;
	block11_data_unref (_data11_);
	_data11_ = NULL;
	return result;
}

void
ft_database_rename_corrupted_file (GFile* database_file,
                                   GError** error)
{
	gchar* stamp = NULL;
	GDateTime* _tmp0_;
	GDateTime* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* destination_path = NULL;
	gchar* _tmp4_;
	gchar* _tmp5_;
	const gchar* _tmp6_;
	const gchar* _tmp7_;
	gchar* _tmp8_;
	gchar* _tmp9_;
	GFile* destination_file = NULL;
	GFile* _tmp10_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (database_file != NULL);
	_tmp0_ = g_date_time_new_now_local ();
	_tmp1_ = _tmp0_;
	_tmp2_ = g_date_time_format (_tmp1_, "%Y-%m-%d");
	_tmp3_ = _tmp2_;
	_g_date_time_unref0 (_tmp1_);
	stamp = _tmp3_;
	_tmp4_ = g_file_get_path (database_file);
	_tmp5_ = _tmp4_;
	_tmp6_ = string_to_string (_tmp5_);
	_tmp7_ = string_to_string (stamp);
	_tmp8_ = g_strconcat (_tmp6_, ".corrupted-", _tmp7_, NULL);
	_tmp9_ = _tmp8_;
	_g_free0 (_tmp5_);
	destination_path = _tmp9_;
	_tmp10_ = g_file_new_for_path (destination_path);
	destination_file = _tmp10_;
	g_file_move (database_file, destination_file, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (destination_file);
		_g_free0 (destination_path);
		_g_free0 (stamp);
		return;
	}
	_g_object_unref0 (destination_file);
	_g_free0 (destination_path);
	_g_free0 (stamp);
}

void
ft_database_restore_database_file (GFile* database_file,
                                   GError** error)
{
	gchar* backup_path = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	GFile* backup_file = NULL;
	GFile* _tmp5_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (database_file != NULL);
	_tmp0_ = g_file_get_path (database_file);
	_tmp1_ = _tmp0_;
	_tmp2_ = string_to_string (_tmp1_);
	_tmp3_ = g_strconcat (_tmp2_, ".backup", NULL);
	_tmp4_ = _tmp3_;
	_g_free0 (_tmp1_);
	backup_path = _tmp4_;
	_tmp5_ = g_file_new_for_path (backup_path);
	backup_file = _tmp5_;
	g_file_copy (backup_file, database_file, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (backup_file);
		_g_free0 (backup_path);
		return;
	}
	_g_object_unref0 (backup_file);
	_g_free0 (backup_path);
}

gchar*
ft_database_build_database_path (void)
{
	gchar* directory_path = NULL;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* result;
	_tmp0_ = g_get_user_data_dir ();
	_tmp1_ = g_build_filename (_tmp0_, PACKAGE_NAME, NULL);
	directory_path = _tmp1_;
	_tmp2_ = g_build_filename (directory_path, "database.sqlite", NULL);
	result = _tmp2_;
	_g_free0 (directory_path);
	return result;
}

gchar*
ft_database_build_backup_path (void)
{
	gchar* _tmp0_;
	gchar* _tmp1_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* result;
	_tmp0_ = ft_database_build_database_path ();
	_tmp1_ = _tmp0_;
	_tmp2_ = string_to_string (_tmp1_);
	_tmp3_ = g_strconcat (_tmp2_, ".backup", NULL);
	_tmp4_ = _tmp3_;
	_g_free0 (_tmp1_);
	result = _tmp4_;
	return result;
}

void
ft_database_delete_file_if_exists (const gchar* path)
{
	GFile* file = NULL;
	GFile* _tmp0_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (path != NULL);
	_tmp0_ = g_file_new_for_path (path);
	file = _tmp0_;
	{
		GFile* _tmp1_;
		_tmp1_ = file;
		if (g_file_query_exists (_tmp1_, NULL)) {
			GFile* _tmp2_;
			_tmp2_ = file;
			g_file_delete (_tmp2_, NULL, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch0_g_error;
			}
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_inner_error0_);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_object_unref0 (file);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	_g_object_unref0 (file);
}

/**
     * Create database backup using SQLite's Backup API.
     *
     * This function internally uses queue_read to run the backup in the
     * GOM worker thread, then blocks the calling thread until completion.
     *
     * The backup is created as a temporary file (backup_path~) and only
     * moved to the final location after successful completion, ensuring
     * we never overwrite a good backup with a broken one.
     */
static Block12Data*
block12_data_ref (Block12Data* _data12_)
{
	g_atomic_int_inc (&_data12_->_ref_count_);
	return _data12_;
}

static void
block12_data_unref (void * _userdata_)
{
	Block12Data* _data12_;
	_data12_ = (Block12Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data12_->_ref_count_)) {
		_g_free0 (_data12_->backup_path);
		g_slice_free (Block12Data, _data12_);
	}
}

static void
__lambda84_ (Block12Data* _data12_,
             GomAdapter* adapter)
{
	gchar* temp_backup_path = NULL;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	gboolean success = FALSE;
	sqlite3* db = NULL;
	sqlite3* _tmp2_;
	sqlite3* backup_db = NULL;
	const gchar* _tmp3_;
	const gchar* _tmp4_;
	sqlite3* _tmp5_ = NULL;
	gint _tmp6_;
	sqlite3_backup* backup = NULL;
	sqlite3* _tmp9_;
	sqlite3* _tmp10_;
	sqlite3_backup* _tmp11_;
	sqlite3_backup* _tmp12_;
	gint _result_ = 0;
	sqlite3_backup* _tmp13_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (adapter != NULL);
	_tmp0_ = string_to_string (_data12_->backup_path);
	_tmp1_ = g_strconcat (_tmp0_, "~", NULL);
	temp_backup_path = _tmp1_;
	success = FALSE;
	_tmp2_ = gom_adapter_get_handle (adapter);
	db = _tmp2_;
	_tmp3_ = temp_backup_path;
	ft_database_delete_file_if_exists (_tmp3_);
	_tmp4_ = temp_backup_path;
	_tmp6_ = sqlite3_open (_tmp4_, &_tmp5_);
	_sqlite3_close0 (backup_db);
	backup_db = _tmp5_;
	if (_tmp6_ != SQLITE_OK) {
		sqlite3* _tmp7_;
		const gchar* _tmp8_;
		_tmp7_ = backup_db;
		_tmp8_ = sqlite3_errmsg (_tmp7_);
		g_warning ("database.vala:229: Failed to open temporary backup database: %s", _tmp8_);
		_sqlite3_close0 (backup_db);
		_g_free0 (temp_backup_path);
		return;
	}
	_tmp9_ = backup_db;
	_tmp10_ = db;
	_tmp11_ = sqlite3_backup_init (_tmp9_, "main", _tmp10_, "main");
	backup = _tmp11_;
	_tmp12_ = backup;
	if (_tmp12_ == NULL) {
		g_warning ("database.vala:237: Failed to initialize backup");
		_sqlite3_backup_finish0 (backup);
		_sqlite3_close0 (backup_db);
		_g_free0 (temp_backup_path);
		return;
	}
	_tmp13_ = backup;
	_result_ = sqlite3_backup_step (_tmp13_, -1);
	if (_result_ == SQLITE_DONE) {
		{
			GFile* temp_backup_file = NULL;
			const gchar* _tmp14_;
			GFile* _tmp15_;
			GFile* backup_file = NULL;
			GFile* _tmp16_;
			GFile* _tmp17_;
			GFile* _tmp18_;
			_tmp14_ = temp_backup_path;
			_tmp15_ = g_file_new_for_path (_tmp14_);
			temp_backup_file = _tmp15_;
			_tmp16_ = g_file_new_for_path (_data12_->backup_path);
			backup_file = _tmp16_;
			_tmp17_ = temp_backup_file;
			_tmp18_ = backup_file;
			g_file_move (_tmp17_, _tmp18_, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				_g_object_unref0 (backup_file);
				_g_object_unref0 (temp_backup_file);
				goto __catch0_g_error;
			}
			success = TRUE;
			_g_object_unref0 (backup_file);
			_g_object_unref0 (temp_backup_file);
		}
		goto __finally0;
		__catch0_g_error:
		{
			GError* _error_ = NULL;
			GError* _tmp19_;
			const gchar* _tmp20_;
			_error_ = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp19_ = _error_;
			_tmp20_ = _tmp19_->message;
			g_warning ("database.vala:254: Failed to move temporary backup to final location: " \
"%s", _tmp20_);
			_g_error_free0 (_error_);
		}
		__finally0:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_sqlite3_backup_finish0 (backup);
			_sqlite3_close0 (backup_db);
			_g_free0 (temp_backup_path);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
	} else {
		g_warning ("database.vala:259: Backup failed with error code: %d", _result_);
	}
	if (!success) {
		const gchar* _tmp21_;
		_tmp21_ = temp_backup_path;
		ft_database_delete_file_if_exists (_tmp21_);
	}
	_sqlite3_backup_finish0 (backup);
	_sqlite3_close0 (backup_db);
	_g_free0 (temp_backup_path);
}

static void
___lambda84__gom_adapter_callback (GomAdapter* adapter,
                                   gpointer self)
{
	__lambda84_ (self, adapter);
	block12_data_unref (self);
}

void
ft_database_create_backup (const gchar* backup_path)
{
	Block12Data* _data12_;
	gchar* _tmp0_;
	GomAdapter* _tmp1_;
	GomAdapter* _tmp2_;
	g_return_if_fail (backup_path != NULL);
	_data12_ = g_slice_new0 (Block12Data);
	_data12_->_ref_count_ = 1;
	_tmp0_ = g_strdup (backup_path);
	_g_free0 (_data12_->backup_path);
	_data12_->backup_path = _tmp0_;
	_tmp1_ = ft_database_adapter;
	if (_tmp1_ == NULL) {
		block12_data_unref (_data12_);
		_data12_ = NULL;
		return;
	}
	_tmp2_ = ft_database_adapter;
	gom_adapter_queue_read (_tmp2_, ___lambda84__gom_adapter_callback, block12_data_ref (_data12_));
	block12_data_unref (_data12_);
	_data12_ = NULL;
}

gboolean
ft_database_should_create_backup (const gchar* backup_path)
{
	GFile* backup_file = NULL;
	GFile* _tmp0_;
	GFile* _tmp1_;
	gboolean _tmp19_ = FALSE;
	GError* _inner_error0_ = NULL;
	gboolean result;
	g_return_val_if_fail (backup_path != NULL, FALSE);
	_tmp0_ = g_file_new_for_path (backup_path);
	backup_file = _tmp0_;
	_tmp1_ = backup_file;
	if (!g_file_query_exists (_tmp1_, NULL)) {
		result = TRUE;
		_g_object_unref0 (backup_file);
		return result;
	}
	{
		GFileInfo* info = NULL;
		GFile* _tmp2_;
		GFileInfo* _tmp3_;
		GDateTime* modification_datetime = NULL;
		GFileInfo* _tmp4_;
		GDateTime* _tmp5_;
		GDateTime* now = NULL;
		GDateTime* _tmp6_;
		gboolean _tmp7_ = FALSE;
		gboolean _tmp8_ = FALSE;
		gboolean _tmp9_ = FALSE;
		GDateTime* _tmp10_;
		_tmp2_ = backup_file;
		_tmp3_ = g_file_query_info (_tmp2_, G_FILE_ATTRIBUTE_TIME_MODIFIED, G_FILE_QUERY_INFO_NONE, NULL, &_inner_error0_);
		info = _tmp3_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp4_ = info;
		_tmp5_ = g_file_info_get_modification_date_time (_tmp4_);
		modification_datetime = _tmp5_;
		_tmp6_ = g_date_time_new_now_local ();
		now = _tmp6_;
		_tmp10_ = modification_datetime;
		if (_tmp10_ == NULL) {
			_tmp9_ = TRUE;
		} else {
			GDateTime* _tmp11_;
			GDateTime* _tmp12_;
			_tmp11_ = modification_datetime;
			_tmp12_ = now;
			_tmp9_ = g_date_time_get_year (_tmp11_) != g_date_time_get_year (_tmp12_);
		}
		if (_tmp9_) {
			_tmp8_ = TRUE;
		} else {
			GDateTime* _tmp13_;
			GDateTime* _tmp14_;
			_tmp13_ = modification_datetime;
			_tmp14_ = now;
			_tmp8_ = g_date_time_get_month (_tmp13_) != g_date_time_get_month (_tmp14_);
		}
		if (_tmp8_) {
			_tmp7_ = TRUE;
		} else {
			GDateTime* _tmp15_;
			GDateTime* _tmp16_;
			_tmp15_ = modification_datetime;
			_tmp16_ = now;
			_tmp7_ = g_date_time_get_day_of_month (_tmp15_) != g_date_time_get_day_of_month (_tmp16_);
		}
		result = _tmp7_;
		_g_date_time_unref0 (now);
		_g_date_time_unref0 (modification_datetime);
		_g_object_unref0 (info);
		_g_object_unref0 (backup_file);
		return result;
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* _error_ = NULL;
		GError* _tmp17_;
		const gchar* _tmp18_;
		_error_ = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp17_ = _error_;
		_tmp18_ = _tmp17_->message;
		g_warning ("database.vala:288: Failed to check last backup time: %s", _tmp18_);
		result = TRUE;
		_g_error_free0 (_error_);
		_g_object_unref0 (backup_file);
		return result;
	}
	__finally0:
	_g_object_unref0 (backup_file);
	g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
	g_clear_error (&_inner_error0_);
	return _tmp19_;
}

static gboolean
__lambda85_ (void)
{
	gchar* backup_path = NULL;
	gchar* _tmp0_;
	const gchar* _tmp1_;
	gboolean result;
	_tmp0_ = ft_database_build_backup_path ();
	backup_path = _tmp0_;
	_tmp1_ = backup_path;
	if (ft_database_should_create_backup (_tmp1_)) {
		const gchar* _tmp2_;
		_tmp2_ = backup_path;
		ft_database_create_backup (_tmp2_);
	}
	result = G_SOURCE_REMOVE;
	_g_free0 (backup_path);
	return result;
}

static gboolean
___lambda85__gsource_func (gpointer self)
{
	gboolean result;
	result = __lambda85_ ();
	return result;
}

void
ft_database_schedule_backup (void)
{
	if (ft_is_test ()) {
		return;
	}
	g_idle_add_full (G_PRIORITY_LOW, ___lambda85__gsource_func, NULL, NULL);
}

static void
__lambda86_ (GomAdapter* _adapter)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (_adapter != NULL);
	{
		gom_adapter_execute_sql (_adapter, "PRAGMA foreign_keys = ON;", &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* _error_ = NULL;
		GError* _tmp0_;
		const gchar* _tmp1_;
		_error_ = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp0_ = _error_;
		_tmp1_ = _tmp0_->message;
		g_warning ("database.vala:389: Failed to enable 'foreign_keys': %s", _tmp1_);
		_g_error_free0 (_error_);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
}

static void
___lambda86__gom_adapter_callback (GomAdapter* adapter,
                                   gpointer self)
{
	__lambda86_ (adapter);
}

static gboolean
_ft_database_migrate_repository_gom_repository_migrator (GomRepository* repository,
                                                         GomAdapter* adapter,
                                                         guint version,
                                                         gpointer self,
                                                         GError** error)
{
	gboolean result;
	result = ft_database_migrate_repository (repository, adapter, version, error);
	return result;
}

void
ft_database_open_repository (GFile* database_file,
                             GomAdapter** adapter,
                             GomRepository** repository)
{
	GomAdapter* _vala_adapter = NULL;
	GomRepository* _vala_repository = NULL;
	GomAdapter* _tmp0_;
	gboolean _tmp12_ = FALSE;
	gboolean _tmp13_ = FALSE;
	GomAdapter* _tmp26_;
	GError* _inner_error0_ = NULL;
	_tmp0_ = gom_adapter_new ();
	_g_object_unref0 (_vala_adapter);
	_vala_adapter = _tmp0_;
	_g_object_unref0 (_vala_repository);
	_vala_repository = NULL;
	{
		if (database_file != NULL) {
			GFile* _tmp1_;
			GFile* _tmp2_;
			GomAdapter* _tmp3_;
			gchar* _tmp4_;
			gchar* _tmp5_;
			_tmp1_ = g_file_get_parent (database_file);
			_tmp2_ = _tmp1_;
			ft_database_make_directory_with_parents (_tmp2_);
			_g_object_unref0 (_tmp2_);
			_tmp3_ = _vala_adapter;
			_tmp4_ = g_file_get_uri (database_file);
			_tmp5_ = _tmp4_;
			gom_adapter_open_sync (_tmp3_, _tmp5_, &_inner_error0_);
			_g_free0 (_tmp5_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch0_g_error;
			}
		} else {
			GomAdapter* _tmp6_;
			_tmp6_ = _vala_adapter;
			gom_adapter_open_sync (_tmp6_, ":memory:", &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch0_g_error;
			}
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* _error_ = NULL;
		GFile* _tmp7_ = NULL;
		gchar* _tmp8_ = NULL;
		GError* _tmp10_;
		const gchar* _tmp11_;
		_error_ = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp7_ = database_file;
		_tmp8_ = NULL;
		if (_tmp7_ != NULL) {
			gchar* _tmp9_;
			_tmp9_ = g_file_get_uri (_tmp7_);
			_g_free0 (_tmp8_);
			_tmp8_ = _tmp9_;
		}
		_tmp10_ = _error_;
		_tmp11_ = _tmp10_->message;
		g_critical ("database.vala:327: Failed to open database '%s': %s", _tmp8_, _tmp11_);
		_g_free0 (_tmp8_);
		_g_error_free0 (_error_);
		if (adapter) {
			*adapter = _vala_adapter;
		} else {
			_g_object_unref0 (_vala_adapter);
		}
		if (repository) {
			*repository = _vala_repository;
		} else {
			_g_object_unref0 (_vala_repository);
		}
		return;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_object_unref0 (_vala_adapter);
		_g_object_unref0 (_vala_repository);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	if (database_file != NULL) {
		_tmp13_ = !ft_is_test ();
	} else {
		_tmp13_ = FALSE;
	}
	if (_tmp13_) {
		GomAdapter* _tmp14_;
		_tmp14_ = _vala_adapter;
		_tmp12_ = !ft_database_check_health (_tmp14_);
	} else {
		_tmp12_ = FALSE;
	}
	if (_tmp12_) {
		gboolean has_backup = FALSE;
		{
			GomAdapter* _tmp15_;
			_tmp15_ = _vala_adapter;
			gom_adapter_close_sync (_tmp15_, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch1_g_error;
			}
			_g_object_unref0 (_vala_adapter);
			_vala_adapter = NULL;
		}
		goto __finally1;
		__catch1_g_error:
		{
			GError* _error_ = NULL;
			GError* _tmp16_;
			const gchar* _tmp17_;
			_error_ = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp16_ = _error_;
			_tmp17_ = _tmp16_->message;
			g_warning ("database.vala:344: Error closing corrupted database: %s", _tmp17_);
			_g_error_free0 (_error_);
		}
		__finally1:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (_vala_adapter);
			_g_object_unref0 (_vala_repository);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
		{
			ft_database_rename_corrupted_file (database_file, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch2_g_error;
			}
		}
		goto __finally2;
		__catch2_g_error:
		{
			GError* _error_ = NULL;
			GError* _tmp18_;
			const gchar* _tmp19_;
			_error_ = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp18_ = _error_;
			_tmp19_ = _tmp18_->message;
			g_critical ("database.vala:352: Could not rename corrupted database: %s", _tmp19_);
			_g_error_free0 (_error_);
			if (adapter) {
				*adapter = _vala_adapter;
			} else {
				_g_object_unref0 (_vala_adapter);
			}
			if (repository) {
				*repository = _vala_repository;
			} else {
				_g_object_unref0 (_vala_repository);
			}
			return;
		}
		__finally2:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (_vala_adapter);
			_g_object_unref0 (_vala_repository);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
		has_backup = FALSE;
		{
			ft_database_restore_database_file (database_file, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch3_g_error;
			}
			has_backup = TRUE;
		}
		goto __finally3;
		__catch3_g_error:
		{
			g_clear_error (&_inner_error0_);
		}
		__finally3:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (_vala_adapter);
			_g_object_unref0 (_vala_repository);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
		{
			GomAdapter* _tmp20_;
			GomAdapter* _tmp21_;
			gchar* _tmp22_;
			gchar* _tmp23_;
			_tmp20_ = gom_adapter_new ();
			_g_object_unref0 (_vala_adapter);
			_vala_adapter = _tmp20_;
			_tmp21_ = _vala_adapter;
			_tmp22_ = g_file_get_uri (database_file);
			_tmp23_ = _tmp22_;
			gom_adapter_open_sync (_tmp21_, _tmp23_, &_inner_error0_);
			_g_free0 (_tmp23_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch4_g_error;
			}
			if (has_backup) {
				g_info ("database.vala:371: Restored database from backup");
			} else {
				g_info ("database.vala:374: No backup available, created a fresh database");
			}
		}
		goto __finally4;
		__catch4_g_error:
		{
			GError* _error_ = NULL;
			GError* _tmp24_;
			const gchar* _tmp25_;
			_error_ = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp24_ = _error_;
			_tmp25_ = _tmp24_->message;
			g_critical ("database.vala:378: Failed to open new database: %s", _tmp25_);
			_g_error_free0 (_error_);
			if (adapter) {
				*adapter = _vala_adapter;
			} else {
				_g_object_unref0 (_vala_adapter);
			}
			if (repository) {
				*repository = _vala_repository;
			} else {
				_g_object_unref0 (_vala_repository);
			}
			return;
		}
		__finally4:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (_vala_adapter);
			_g_object_unref0 (_vala_repository);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
	}
	_tmp26_ = _vala_adapter;
	gom_adapter_queue_write (_tmp26_, ___lambda86__gom_adapter_callback, NULL);
	{
		GomAdapter* _tmp27_;
		GomRepository* _tmp28_;
		GomRepository* _tmp29_;
		_tmp27_ = _vala_adapter;
		_tmp28_ = gom_repository_new (_tmp27_);
		_g_object_unref0 (_vala_repository);
		_vala_repository = _tmp28_;
		_tmp29_ = _vala_repository;
		gom_repository_migrate_sync (_tmp29_, FT_DATABASE_VERSION, _ft_database_migrate_repository_gom_repository_migrator, NULL, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch5_g_error;
		}
	}
	goto __finally5;
	__catch5_g_error:
	{
		GError* _error_ = NULL;
		const gchar* _tmp30_;
		_error_ = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp30_ = _error_->message;
		g_error ("database.vala:399: Failed to migrate database: %s", _tmp30_);
		_g_error_free0 (_error_);
	}
	__finally5:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_object_unref0 (_vala_adapter);
		_g_object_unref0 (_vala_repository);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	if (adapter) {
		*adapter = _vala_adapter;
	} else {
		_g_object_unref0 (_vala_adapter);
	}
	if (repository) {
		*repository = _vala_repository;
	} else {
		_g_object_unref0 (_vala_repository);
	}
}

void
ft_database_open (void)
{
	GFile* database_file = NULL;
	GomRepository* _tmp0_;
	GFile* _tmp25_;
	GomAdapter* _tmp26_ = NULL;
	GomRepository* _tmp27_ = NULL;
	GError* _inner_error0_ = NULL;
	database_file = NULL;
	_tmp0_ = ft_database_repository;
	if (_tmp0_ != NULL) {
		_g_object_unref0 (database_file);
		return;
	}
	if (!ft_is_test ()) {
		gchar* database_path = NULL;
		gchar* _tmp1_;
		const gchar* _tmp2_;
		GFile* _tmp3_;
		GFile* directory_file = NULL;
		GFile* _tmp4_;
		GFile* _tmp5_;
		gboolean _tmp6_ = FALSE;
		GFile* _tmp7_;
		gchar* old_database_path = NULL;
		GFile* old_database_file = NULL;
		const gchar* _tmp14_;
		GFile* _tmp15_;
		gboolean _tmp16_ = FALSE;
		GFile* _tmp17_;
		_tmp1_ = ft_database_build_database_path ();
		database_path = _tmp1_;
		_tmp2_ = database_path;
		_tmp3_ = g_file_new_for_path (_tmp2_);
		_g_object_unref0 (database_file);
		database_file = _tmp3_;
		_tmp4_ = database_file;
		_tmp5_ = g_file_get_parent (_tmp4_);
		directory_file = _tmp5_;
		_tmp7_ = directory_file;
		if (_tmp7_ != NULL) {
			GFile* _tmp8_;
			_tmp8_ = directory_file;
			_tmp6_ = !g_file_query_exists (_tmp8_, NULL);
		} else {
			_tmp6_ = FALSE;
		}
		if (_tmp6_) {
			GFile* _tmp9_;
			_tmp9_ = directory_file;
			ft_database_make_directory_with_parents (_tmp9_);
		}
		if (ft_is_flatpak ()) {
			const gchar* _tmp10_;
			gchar* _tmp11_;
			_tmp10_ = g_get_home_dir ();
			_tmp11_ = g_build_filename (_tmp10_, ".local", "share", "gnome-pomodoro", "database.sqlite", NULL);
			_g_free0 (old_database_path);
			old_database_path = _tmp11_;
		} else {
			const gchar* _tmp12_;
			gchar* _tmp13_;
			_tmp12_ = g_get_user_data_dir ();
			_tmp13_ = g_build_filename (_tmp12_, "gnome-pomodoro", "database.sqlite", NULL);
			_g_free0 (old_database_path);
			old_database_path = _tmp13_;
		}
		_tmp14_ = old_database_path;
		_tmp15_ = g_file_new_for_path (_tmp14_);
		old_database_file = _tmp15_;
		_tmp17_ = database_file;
		if (!g_file_query_exists (_tmp17_, NULL)) {
			GFile* _tmp18_;
			_tmp18_ = old_database_file;
			_tmp16_ = g_file_query_exists (_tmp18_, NULL);
		} else {
			_tmp16_ = FALSE;
		}
		if (_tmp16_) {
			{
				GFile* _tmp19_;
				GFile* _tmp20_;
				const gchar* _tmp21_;
				const gchar* _tmp22_;
				_tmp19_ = old_database_file;
				_tmp20_ = database_file;
				g_file_copy (_tmp19_, _tmp20_, G_FILE_COPY_NONE, NULL, NULL, NULL, &_inner_error0_);
				if (G_UNLIKELY (_inner_error0_ != NULL)) {
					goto __catch0_g_error;
				}
				_tmp21_ = old_database_path;
				_tmp22_ = database_path;
				g_info ("database.vala:444: Imported database from %s to %s", _tmp21_, _tmp22_);
			}
			goto __finally0;
			__catch0_g_error:
			{
				GError* _error_ = NULL;
				GError* _tmp23_;
				const gchar* _tmp24_;
				_error_ = _inner_error0_;
				_inner_error0_ = NULL;
				_tmp23_ = _error_;
				_tmp24_ = _tmp23_->message;
				g_warning ("database.vala:447: Failed to import database: %s", _tmp24_);
				_g_error_free0 (_error_);
			}
			__finally0:
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				_g_object_unref0 (old_database_file);
				_g_free0 (old_database_path);
				_g_object_unref0 (directory_file);
				_g_free0 (database_path);
				_g_object_unref0 (database_file);
				g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
				g_clear_error (&_inner_error0_);
				return;
			}
		}
		_g_object_unref0 (old_database_file);
		_g_free0 (old_database_path);
		_g_object_unref0 (directory_file);
		_g_free0 (database_path);
	}
	_tmp25_ = database_file;
	ft_database_open_repository (_tmp25_, &_tmp26_, &_tmp27_);
	_g_object_unref0 (ft_database_adapter);
	ft_database_adapter = _tmp26_;
	_g_object_unref0 (ft_database_repository);
	ft_database_repository = _tmp27_;
	_g_object_unref0 (database_file);
}

void
ft_database_close_repository (GomRepository* repository)
{
	GomAdapter* adapter = NULL;
	GomAdapter* _tmp0_;
	GomAdapter* _tmp1_;
	GomAdapter* _tmp2_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (repository != NULL);
	_tmp0_ = gom_repository_get_adapter (repository);
	_tmp1_ = _tmp0_;
	_tmp2_ = _g_object_ref0 (_tmp1_);
	adapter = _tmp2_;
	{
		gom_adapter_close_sync (adapter, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* _error_ = NULL;
		GError* _tmp3_;
		const gchar* _tmp4_;
		_error_ = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp3_ = _error_;
		_tmp4_ = _tmp3_->message;
		g_warning ("database.vala:465: Error while closing database: %s", _tmp4_);
		_g_error_free0 (_error_);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_object_unref0 (adapter);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	_g_object_unref0 (adapter);
}

void
ft_database_close (void)
{
	GomRepository* _tmp0_;
	GomRepository* _tmp1_;
	_tmp0_ = ft_database_repository;
	if (_tmp0_ == NULL) {
		return;
	}
	_tmp1_ = ft_database_repository;
	ft_database_close_repository (_tmp1_);
	_g_object_unref0 (ft_database_repository);
	ft_database_repository = NULL;
	_g_object_unref0 (ft_database_adapter);
	ft_database_adapter = NULL;
}

gchar*
ft_database_serialize_date (GDate* date)
{
	gchar* _tmp0_ = NULL;
	gchar* _tmp4_;
	gchar* result;
	g_return_val_if_fail (date != NULL, NULL);
	if (g_date_valid (date)) {
		GDate _tmp1_;
		gchar* _tmp2_;
		_tmp1_ = *date;
		_tmp2_ = ft_date_utils_format_date (&_tmp1_, FT_DATABASE_DATE_FORMAT);
		_g_free0 (_tmp0_);
		_tmp0_ = _tmp2_;
	} else {
		gchar* _tmp3_;
		_tmp3_ = g_strdup ("");
		_g_free0 (_tmp0_);
		_tmp0_ = _tmp3_;
	}
	_tmp4_ = _tmp0_;
	_tmp0_ = NULL;
	result = _tmp4_;
	_g_free0 (_tmp0_);
	return result;
}

/**
     * Remove leading zeros from a string
     */
static gchar
string_get (const gchar* self,
            glong index)
{
	gchar _tmp0_;
	gchar result;
	g_return_val_if_fail (self != NULL, '\0');
	_tmp0_ = ((gchar*) self)[index];
	result = _tmp0_;
	return result;
}

static glong
string_strnlen (gchar* str,
                glong maxlen)
{
	gchar* end = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	glong result;
	_tmp0_ = memchr (str, 0, (gsize) maxlen);
	end = _tmp0_;
	_tmp1_ = end;
	if (_tmp1_ == NULL) {
		result = maxlen;
		return result;
	} else {
		gchar* _tmp2_;
		_tmp2_ = end;
		result = (glong) (_tmp2_ - str);
		return result;
	}
}

static gchar*
string_substring (const gchar* self,
                  glong offset,
                  glong len)
{
	glong string_length = 0L;
	gboolean _tmp0_ = FALSE;
	gchar* _tmp3_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	if (offset >= ((glong) 0)) {
		_tmp0_ = len >= ((glong) 0);
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		string_length = string_strnlen ((gchar*) self, offset + len);
	} else {
		gint _tmp1_;
		gint _tmp2_;
		_tmp1_ = strlen (self);
		_tmp2_ = _tmp1_;
		string_length = (glong) _tmp2_;
	}
	if (offset < ((glong) 0)) {
		offset = string_length + offset;
		g_return_val_if_fail (offset >= ((glong) 0), NULL);
	} else {
		g_return_val_if_fail (offset <= string_length, NULL);
	}
	if (len < ((glong) 0)) {
		len = string_length - offset;
	}
	g_return_val_if_fail ((offset + len) <= string_length, NULL);
	_tmp3_ = g_strndup (((gchar*) self) + offset, (gsize) len);
	result = _tmp3_;
	return result;
}

inline gchar*
ft_database_chug_zeros (const gchar* str)
{
	gint index = 0;
	gchar* _tmp1_ = NULL;
	gchar* _tmp4_;
	gchar* result;
	g_return_val_if_fail (str != NULL, NULL);
	index = 0;
	while (TRUE) {
		gint _tmp0_;
		if (!(string_get (str, (glong) index) == '0')) {
			break;
		}
		_tmp0_ = index;
		index = _tmp0_ + 1;
	}
	if (index > 0) {
		gchar* _tmp2_;
		_tmp2_ = string_substring (str, (glong) index, (glong) -1);
		_g_free0 (_tmp1_);
		_tmp1_ = _tmp2_;
	} else {
		gchar* _tmp3_;
		_tmp3_ = g_strdup (str);
		_g_free0 (_tmp1_);
		_tmp1_ = _tmp3_;
	}
	_tmp4_ = _tmp1_;
	_tmp1_ = NULL;
	result = _tmp4_;
	_g_free0 (_tmp1_);
	return result;
}

static guint
uint_parse (const gchar* str,
            guint _base)
{
	guint result;
	g_return_val_if_fail (str != NULL, 0U);
	result = (guint) strtoul (str, NULL, (gint) _base);
	return result;
}

void
ft_database_parse_date (const gchar* date_string,
                        GDate* result)
{
	gchar** parts = NULL;
	gchar** _tmp0_;
	gchar** _tmp1_;
	gint parts_length1;
	gint _parts_size_;
	GDate date = {0};
	gchar** _tmp2_;
	gint _tmp2__length1;
	g_return_if_fail (date_string != NULL);
	_tmp1_ = _tmp0_ = g_strsplit (date_string, "-", 0);
	parts = _tmp1_;
	parts_length1 = _vala_array_length (_tmp0_);
	_parts_size_ = parts_length1;
	memset (&date, 0, sizeof (GDate));
	_tmp2_ = parts;
	_tmp2__length1 = parts_length1;
	if (_tmp2__length1 == 3) {
		guint year = 0U;
		gchar** _tmp3_;
		gint _tmp3__length1;
		const gchar* _tmp4_;
		gchar* _tmp5_;
		gchar* _tmp6_;
		guint _tmp7_;
		guint month = 0U;
		gchar** _tmp8_;
		gint _tmp8__length1;
		const gchar* _tmp9_;
		gchar* _tmp10_;
		gchar* _tmp11_;
		guint _tmp12_;
		guint day = 0U;
		gchar** _tmp13_;
		gint _tmp13__length1;
		const gchar* _tmp14_;
		gchar* _tmp15_;
		gchar* _tmp16_;
		guint _tmp17_;
		_tmp3_ = parts;
		_tmp3__length1 = parts_length1;
		_tmp4_ = _tmp3_[0];
		_tmp5_ = ft_database_chug_zeros (_tmp4_);
		_tmp6_ = _tmp5_;
		_tmp7_ = uint_parse (_tmp6_, (guint) 0);
		_g_free0 (_tmp6_);
		year = _tmp7_;
		_tmp8_ = parts;
		_tmp8__length1 = parts_length1;
		_tmp9_ = _tmp8_[1];
		_tmp10_ = ft_database_chug_zeros (_tmp9_);
		_tmp11_ = _tmp10_;
		_tmp12_ = uint_parse (_tmp11_, (guint) 0);
		_g_free0 (_tmp11_);
		month = _tmp12_;
		_tmp13_ = parts;
		_tmp13__length1 = parts_length1;
		_tmp14_ = _tmp13_[2];
		_tmp15_ = ft_database_chug_zeros (_tmp14_);
		_tmp16_ = _tmp15_;
		_tmp17_ = uint_parse (_tmp16_, (guint) 0);
		_g_free0 (_tmp16_);
		day = _tmp17_;
		g_date_set_dmy (&date, (GDateDay) day, (gint) ((GDateMonth) month), (GDateYear) year);
	}
	*result = date;
	parts = (_vala_array_free (parts, parts_length1, (GDestroyNotify) g_free), NULL);
	return;
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

static gssize
_vala_array_length (gpointer array)
{
	gssize length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}

