/*
 * Copyright (C) 2022-2025 Seagull Developers
 *
 * Seagull is the legal property of its developers, whose names are too
 * numerous to list here. Please refer to the AUTHORS file distributed
 * with this source distribution
 *
 * This library 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 2.1 of the License, or (at your option) any later version.
 *
 * This library 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 this library; if not, see <https://www.gnu.org/licenses/>.
 */

#include <sqlite3.h>

#include <seagull.h>

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_seagull_sqlite3_column_name_named(void) {
	SeagullSqlite3 *db = NULL;
	SeagullStatement *stmt = NULL;
	SeagullSqlite3Stmt *sql_stmt = NULL;
	GError *error = NULL;
	gboolean result = FALSE;
	const char *column_name = NULL;
	const char *migrations[] = {
		"CREATE TABLE test(num INTEGER);",
		"INSERT INTO test(num) VALUES(42);",
		NULL
	};

	db = seagull_db_new_in_memory(&error);
	g_assert_no_error(error);
	g_assert_nonnull(db);

	result = seagull_migrations_run_from_strings(db, migrations, &error);
	g_assert_no_error(error);
	g_assert_true(result);

	stmt = seagull_statement_new(db, "SELECT num AS col1 FROM test", &error);
	g_assert_no_error(error);
	g_assert_true(SEAGULL_IS_STATEMENT(stmt));

	result = seagull_statement_step(stmt, &error);
	g_assert_no_error(error);
	g_assert_true(result);

	/* We need to use the sqlite3 statement so that we can use a numbered
	 * index.
	 */
	sql_stmt = seagull_statement_get_stmt(stmt);
	column_name = sqlite3_column_name(sql_stmt, 0);
	g_assert_cmpstr(column_name, ==, "col1");

	g_clear_object(&stmt);

	result = seagull_db_close(db, &error);
	g_assert_no_error(error);
	g_assert_true(result);
}

static void
test_seagull_sqlite3_column_name_unnamed(void) {
	SeagullSqlite3 *db = NULL;
	SeagullStatement *stmt = NULL;
	SeagullSqlite3Stmt *sql_stmt = NULL;
	GError *error = NULL;
	gboolean result = FALSE;
	const char *column_name = NULL;
	const char *migrations[] = {
		"CREATE TABLE test(num INTEGER);",
		"INSERT INTO test(num) VALUES(42);",
		NULL
	};

	db = seagull_db_new_in_memory(&error);
	g_assert_no_error(error);
	g_assert_nonnull(db);

	result = seagull_migrations_run_from_strings(db, migrations, &error);
	g_assert_no_error(error);
	g_assert_true(result);

	stmt = seagull_statement_new(db, "SELECT num FROM test", &error);
	g_assert_no_error(error);
	g_assert_true(SEAGULL_IS_STATEMENT(stmt));

	result = seagull_statement_step(stmt, &error);
	g_assert_no_error(error);
	g_assert_true(result);

	/* We need to use the sqlite3 statement so that we can use a numbered
	 * index. We currently expect this to return the column name if no "AS"
	 * keyword was specified.
	 */
	sql_stmt = seagull_statement_get_stmt(stmt);
	column_name = sqlite3_column_name(sql_stmt, 0);
	g_assert_cmpstr(column_name, ==, "num");

	g_clear_object(&stmt);

	result = seagull_db_close(db, &error);
	g_assert_no_error(error);
	g_assert_true(result);
}

/******************************************************************************
 * Main
 *****************************************************************************/
int
main(int argc, char *argv[]) {
	g_test_init(&argc, &argv, NULL);

	g_test_add_func("/sqlite3/column-name/named",
	                test_seagull_sqlite3_column_name_named);
	g_test_add_func("/sqlite3/column-name/unnamed",
	                test_seagull_sqlite3_column_name_unnamed);

	return g_test_run();
}
