/**
 * @file mysql_watchdog_test-t.cpp
 * @brief Test ProxySQL watchdog heartbeat for MySQL threads.
 * @details This test runs ProxySQL's internal test commands:
 *  - PROXYSQLTEST 55 0 → MySQL watchdog test
 */

#include <string>
#include <string.h>
#include "mysql.h"
#include "tap.h"
#include "command_line.h"
#include "noise_utils.h"

using std::string;

CommandLine cl;

bool run_watchdog_test(MYSQL* conn, int test_arg, const char* test_label) {
	char query[64];
	snprintf(query, sizeof(query), "PROXYSQLTEST 55 %d", test_arg);

	diag("Running %s watchdog test...", test_label);
	if (mysql_query(conn, query)) {
		std::string error_msg = mysql_error(conn);
		if (error_msg.find("Invalid test") != std::string::npos) {
			ok(true, "ProxySQL is not compiled in Debug mode. Skipping %s watchdog test", test_label);
			return true;
		} else {
			diag("Error running %s watchdog test: %s", test_label, error_msg.c_str());
			ok(false, "Watchdog test %s failed", test_label);
			return false;
		}
	}
	ok(true, "%s watchdog test executed successfully", test_label);
	return true;
}

int main() {
	if (cl.getEnv()) {
		diag("Failed to get required environment variables");
		return -1;
	}

	spawn_internal_noise(cl, internal_noise_random_stats_poller);
	spawn_internal_noise(cl, internal_noise_rest_prometheus_poller, {{"enable_rest_api", "true"}});
	spawn_internal_noise(cl, internal_noise_pgsql_traffic_v2, {{"num_connections", "100"}, {"reconnect_interval", "100"}, {"avg_delay_ms", "300"}});

	if (cl.use_noise) {
		plan(1 + 3);
	} else {
		plan(1);
	}

	MYSQL* proxysql_admin = mysql_init(NULL);
	if (!proxysql_admin) {
		fprintf(stderr, "Error: mysql_init failed\n");
		return -1;
	}

	if (!mysql_real_connect(proxysql_admin, cl.host, cl.admin_username, cl.admin_password,
		NULL, cl.admin_port, NULL, 0)) {
		fprintf(stderr, "Connection error: %s\n", mysql_error(proxysql_admin));
		return -1;
	}

	run_watchdog_test(proxysql_admin, 0, "MySQL");

	mysql_close(proxysql_admin);
	return exit_status();
}
