#include "ssb.js.h"

#include "log.h"
#include "mem.h"
#include "ssb.db.h"
#include "ssb.ebt.h"
#include "ssb.h"
#include "util.js.h"

#include "sodium/crypto_sign.h"
#include "sqlite3.h"
#include "string.h"
#include "uv.h"

#include <inttypes.h>

static void _tf_ssb_on_blob_stored_callback(tf_ssb_t* ssb, const char* id, void* user_data);

static const int k_sql_async_timeout_ms = 60 * 1000;

static JSClassID _tf_ssb_classId;

static JSValue _tf_ssb_getServerIdentity(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{
	JSValue result = JS_UNDEFINED;
	tf_ssb_t* ssb = JS_GetOpaque(data[0], _tf_ssb_classId);
	if (ssb)
	{
		char id[k_id_base64_len] = { 0 };
		if (tf_ssb_whoami(ssb, id, sizeof(id)))
		{
			result = JS_NewString(context, id);
		}
	}
	return result;
}

typedef struct _blob_get_t
{
	JSContext* context;
	JSValue promise[2];
} blob_get_t;

static void _tf_ssb_blobGet_callback(bool found, const uint8_t* data, size_t size, void* user_data)
{
	blob_get_t* get = user_data;
	JSValue result = JS_UNDEFINED;
	if (found)
	{
		result = JS_NewArrayBufferCopy(get->context, data, size);
	}
	JSValue error = JS_Call(get->context, get->promise[0], JS_UNDEFINED, 1, &result);
	JS_FreeValue(get->context, result);
	JS_FreeValue(get->context, get->promise[0]);
	JS_FreeValue(get->context, get->promise[1]);
	tf_util_report_error(get->context, error);
	JS_FreeValue(get->context, error);
	tf_free(get);
}

static JSValue _tf_ssb_blobGet(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{
	JSValue result = JS_NULL;
	tf_ssb_t* ssb = JS_GetOpaque(data[0], _tf_ssb_classId);
	if (ssb)
	{
		const char* id = JS_ToCString(context, argv[0]);
		blob_get_t* get = tf_malloc(sizeof(blob_get_t));
		*get = (blob_get_t) { .context = context };
		result = JS_NewPromiseCapability(context, get->promise);
		tf_ssb_db_blob_get_async(ssb, id, _tf_ssb_blobGet_callback, get);
		JS_FreeCString(context, id);
	}
	return result;
}

typedef struct _blob_has_t
{
	JSContext* context;
	char id[k_id_base64_len];
	bool found;
	JSValue promise[2];
} blob_has_t;

static void _tf_ssb_blob_has_work(tf_ssb_t* ssb, void* user_data)
{
	blob_has_t* work = user_data;
	sqlite3* db = tf_ssb_acquire_db_reader(ssb);
	work->found = tf_ssb_db_blob_has(db, work->id);
	tf_ssb_release_db_reader(ssb, db);
}

static void _tf_ssb_blob_has_after_work(tf_ssb_t* ssb, int status, void* user_data)
{
	blob_has_t* work = user_data;
	JSValue result = work->found ? JS_TRUE : JS_FALSE;
	JSValue error = JS_Call(work->context, work->promise[0], JS_UNDEFINED, 1, &result);
	JS_FreeValue(work->context, result);
	JS_FreeValue(work->context, work->promise[0]);
	JS_FreeValue(work->context, work->promise[1]);
	tf_util_report_error(work->context, error);
	JS_FreeValue(work->context, error);
	tf_free(work);
}

static JSValue _tf_ssb_blobHas(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{
	JSValue result = JS_NULL;
	tf_ssb_t* ssb = JS_GetOpaque(data[0], _tf_ssb_classId);
	if (ssb)
	{
		const char* id = JS_ToCString(context, argv[0]);
		blob_has_t* work = tf_malloc(sizeof(blob_has_t));
		*work = (blob_has_t) { .context = context };
		tf_string_set(work->id, sizeof(work->id), id);
		result = JS_NewPromiseCapability(context, work->promise);
		tf_ssb_run_work(ssb, _tf_ssb_blob_has_work, _tf_ssb_blob_has_after_work, work);
		JS_FreeCString(context, id);
	}
	return result;
}

typedef struct _blob_store_t
{
	JSContext* context;
	JSValue promise[2];
	uint8_t* buffer;
} blob_store_t;

static void _tf_ssb_blob_store_complete(blob_store_t* store, const char* id)
{
	JSValue result = JS_UNDEFINED;
	if (id)
	{
		JSValue id_value = JS_NewString(store->context, id);
		JSValue result = JS_Call(store->context, store->promise[0], JS_UNDEFINED, 1, &id_value);
		JS_FreeValue(store->context, id_value);
		tf_util_report_error(store->context, result);
		JS_FreeValue(store->context, result);
	}
	else
	{
		JSValue result = JS_Call(store->context, store->promise[1], JS_UNDEFINED, 0, NULL);
		tf_util_report_error(store->context, result);
		JS_FreeValue(store->context, result);
	}
	tf_util_report_error(store->context, result);
	JS_FreeValue(store->context, result);
	JS_FreeValue(store->context, store->promise[0]);
	JS_FreeValue(store->context, store->promise[1]);
	tf_free(store->buffer);
	tf_free(store);
}

static void _tf_ssb_blob_store_callback(const char* id, bool is_new, void* user_data)
{
	blob_store_t* store = user_data;
	_tf_ssb_blob_store_complete(store, id);
}

static JSValue _tf_ssb_blobStore(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{
	blob_store_t* store = tf_malloc(sizeof(blob_store_t));
	*store = (blob_store_t) { .context = context };
	JSValue result = JS_NewPromiseCapability(context, store->promise);

	tf_ssb_t* ssb = JS_GetOpaque(data[0], _tf_ssb_classId);
	if (ssb)
	{
		uint8_t* blob = NULL;
		size_t size = 0;
		if (JS_IsString(argv[0]))
		{
			const char* text = JS_ToCStringLen(context, &size, argv[0]);
			store->buffer = tf_malloc(size);
			memcpy(store->buffer, text, size);
			tf_ssb_db_blob_store_async(ssb, store->buffer, size, _tf_ssb_blob_store_callback, store);
			JS_FreeCString(context, text);
		}
		else if ((blob = tf_util_try_get_array_buffer(context, &size, argv[0])) != 0)
		{
			store->buffer = tf_malloc(size);
			memcpy(store->buffer, blob, size);
			tf_ssb_db_blob_store_async(ssb, store->buffer, size, _tf_ssb_blob_store_callback, store);
		}
		else
		{
			size_t offset;
			size_t element_size;
			JSValue buffer = tf_util_try_get_typed_array_buffer(context, argv[0], &offset, &size, &element_size);
			if (!JS_IsException(buffer))
			{
				blob = tf_util_try_get_array_buffer(context, &size, buffer);
				if (blob)
				{
					store->buffer = tf_malloc(size);
					memcpy(store->buffer, blob, size);
					tf_ssb_db_blob_store_async(ssb, store->buffer, size, _tf_ssb_blob_store_callback, store);
				}
				else
				{
					_tf_ssb_blob_store_complete(store, NULL);
				}
			}
			else
			{
				_tf_ssb_blob_store_complete(store, NULL);
			}
			JS_FreeValue(context, buffer);
		}
	}
	return result;
}

static JSValue _tf_ssb_connections(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{
	JSValue result = JS_NULL;
	tf_ssb_t* ssb = JS_GetOpaque(data[0], _tf_ssb_classId);
	if (ssb)
	{
		tf_ssb_connection_t* connections[32];
		int count = tf_ssb_get_connections(ssb, connections, tf_countof(connections));

		result = JS_NewArray(context);
		for (int i = 0; i < count; i++)
		{
			char id[k_id_base64_len] = { 0 };
			tf_ssb_connection_t* connection = connections[i];
			JSValue object = JS_NewObject(context);
			tf_ssb_connection_get_id(connection, id, sizeof(id));
			JS_SetPropertyStr(context, object, "id", JS_NewString(context, id));
			JS_SetPropertyStr(context, object, "host", JS_NewString(context, tf_ssb_connection_get_host(connection)));
			JS_SetPropertyStr(context, object, "port", JS_NewInt32(context, tf_ssb_connection_get_port(connection)));
			int tunnel_index = tf_ssb_connection_get_tunnel_index(connection);
			if (tunnel_index >= 0)
			{
				for (int j = 0; j < count; j++)
				{
					if (tf_ssb_connection_get_index(connections[j]) == tunnel_index)
					{
						tunnel_index = j;
						break;
					}
				}
				JS_SetPropertyStr(context, object, "tunnel", JS_NewInt32(context, tunnel_index));
			}
			JS_SetPropertyStr(context, object, "requests", tf_ssb_connection_requests_to_object(connection));
			JSValue flags_object = JS_NewObject(context);
			int flags = tf_ssb_connection_get_flags(connection);
			JS_SetPropertyStr(context, flags_object, "one_shot", JS_NewBool(context, (flags & k_tf_ssb_connect_flag_one_shot) != 0));
			JS_SetPropertyStr(context, object, "flags", flags_object);
			JS_SetPropertyStr(context, object, "connected", JS_NewBool(context, tf_ssb_connection_is_connected(connection)));
			const char* destroy_reason = tf_ssb_connection_get_destroy_reason(connection);
			if (destroy_reason)
			{
				JS_SetPropertyStr(context, object, "destroy_reason", JS_NewString(context, destroy_reason));
			}
			int in = 0;
			int out = 0;
			int max_in = 0;
			int max_out = 0;
			tf_ssb_ebt_get_progress(tf_ssb_connection_get_ebt(connection), &in, &max_in, &out, &max_out);
			JSValue progress = JS_NewObject(context);
			JSValue in_progress = JS_NewObject(context);
			JS_SetPropertyStr(context, in_progress, "current", JS_NewInt32(context, in));
			JS_SetPropertyStr(context, in_progress, "total", JS_NewInt32(context, max_in));
			JS_SetPropertyStr(context, progress, "in", in_progress);
			JSValue out_progress = JS_NewObject(context);
			JS_SetPropertyStr(context, out_progress, "current", JS_NewInt32(context, out));
			JS_SetPropertyStr(context, out_progress, "total", JS_NewInt32(context, max_out));
			JS_SetPropertyStr(context, progress, "out", out_progress);
			JS_SetPropertyStr(context, object, "progress", progress);
			JS_SetPropertyUint32(context, result, i, object);
		}
	}
	return result;
}

typedef struct _stored_connections_t
{
	int count;
	tf_ssb_db_stored_connection_t* connections;
	JSValue promise[2];
} stored_connections_t;

static void _tf_ssb_stored_connections_work(tf_ssb_t* ssb, void* user_data)
{
	stored_connections_t* work = user_data;
	work->connections = tf_ssb_db_get_stored_connections(ssb, &work->count);
}

static void _tf_ssb_stored_connections_after_work(tf_ssb_t* ssb, int status, void* user_data)
{
	stored_connections_t* work = user_data;
	JSContext* context = tf_ssb_get_context(ssb);
	JSValue result = JS_NewArray(context);
	for (int i = 0; i < work->count; i++)
	{
		JSValue connection = JS_NewObject(context);
		JS_SetPropertyStr(context, connection, "address", JS_NewString(context, work->connections[i].address));
		JS_SetPropertyStr(context, connection, "port", JS_NewInt32(context, work->connections[i].port));
		JS_SetPropertyStr(context, connection, "pubkey", JS_NewString(context, work->connections[i].pubkey));
		JS_SetPropertyStr(context, connection, "last_attempt", JS_NewInt64(context, work->connections[i].last_attempt));
		JS_SetPropertyStr(context, connection, "last_success", JS_NewInt64(context, work->connections[i].last_success));
		JS_SetPropertyUint32(context, result, i, connection);
	}
	tf_free(work->connections);

	JSValue error = JS_Call(context, work->promise[0], JS_UNDEFINED, 1, &result);
	JS_FreeValue(context, result);
	JS_FreeValue(context, work->promise[0]);
	JS_FreeValue(context, work->promise[1]);
	tf_util_report_error(context, error);
	JS_FreeValue(context, error);
	tf_free(work);
}

static JSValue _tf_ssb_storedConnections(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{
	JSValue result = JS_NULL;
	tf_ssb_t* ssb = JS_GetOpaque(data[0], _tf_ssb_classId);
	if (ssb)
	{
		stored_connections_t* work = tf_malloc(sizeof(stored_connections_t));
		*work = (stored_connections_t) { 0 };
		result = JS_NewPromiseCapability(context, work->promise);
		tf_ssb_run_work(ssb, _tf_ssb_stored_connections_work, _tf_ssb_stored_connections_after_work, work);
	}
	return result;
}

static JSValue _tf_ssb_getConnection(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{
	tf_ssb_t* ssb = JS_GetOpaque(data[0], _tf_ssb_classId);
	const char* id = JS_ToCString(context, argv[0]);
	tf_ssb_connection_t* connection = tf_ssb_connection_get(ssb, id);
	JS_FreeCString(context, id);
	return JS_DupValue(context, tf_ssb_connection_get_object(connection));
}

static JSValue _tf_ssb_closeConnection(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{
	tf_ssb_t* ssb = JS_GetOpaque(data[0], _tf_ssb_classId);
	const char* id = JS_ToCString(context, argv[0]);
	tf_ssb_connection_t* connection = tf_ssb_connection_get(ssb, id);
	if (connection)
	{
		tf_ssb_connection_close(connection, "Closed by user");
	}
	JS_FreeCString(context, id);
	return connection ? JS_TRUE : JS_FALSE;
}

typedef struct _sql_work_t
{
	tf_ssb_t* ssb;
	sqlite3* db;
	char* error;
	const char* query;
	uint8_t* binds;
	uint8_t* rows;
	size_t binds_count;
	size_t rows_count;
	uv_async_t async;
	uv_timer_t timeout;
	uv_mutex_t lock;
	JSValue callback;
	JSValue promise[2];
	int result;
} sql_work_t;

static void _tf_ssb_sql_append(uint8_t** rows, size_t* rows_count, const void* data, size_t size)
{
	*rows = tf_resize_vec(*rows, *rows_count + size);
	memcpy(*rows + *rows_count, data, size);
	*rows_count += size;
}

static void _tf_ssb_sqlAsync_work(tf_ssb_t* ssb, void* user_data)
{
	sql_work_t* sql_work = user_data;
	sqlite3* db = tf_ssb_acquire_db_reader_restricted(ssb);
	uv_mutex_lock(&sql_work->lock);
	sql_work->db = db;
	uv_mutex_unlock(&sql_work->lock);
	uv_async_send(&sql_work->async);
	sqlite3_stmt* statement = NULL;
	sql_work->result = sqlite3_prepare_v2(db, sql_work->query, -1, &statement, NULL);
	if (sql_work->result == SQLITE_OK && statement)
	{
		const uint8_t* p = sql_work->binds;
		int column = 0;
		while (p < sql_work->binds + sql_work->binds_count)
		{
			switch (*p++)
			{
			case SQLITE_INTEGER:
				{
					int64_t value = 0;
					memcpy(&value, p, sizeof(value));
					sqlite3_bind_int64(statement, column + 1, value);
					p += sizeof(value);
				}
				break;
			case SQLITE_TEXT:
				{
					size_t length = 0;
					memcpy(&length, p, sizeof(length));
					p += sizeof(length);
					sqlite3_bind_text(statement, column + 1, (const char*)p, length, NULL);
					p += length;
				}
				break;
			case SQLITE_NULL:
				sqlite3_bind_null(statement, column + 1);
				break;
			default:
				abort();
			}
			column++;
		}
		int r = SQLITE_OK;
		while ((r = sqlite3_step(statement)) == SQLITE_ROW)
		{
			_tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, &(uint8_t[]) { 'r' }, 1);
			for (int i = 0; i < sqlite3_column_count(statement); i++)
			{
				_tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, &(uint8_t[]) { 'c' }, 1);
				const char* name = sqlite3_column_name(statement, i);
				_tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, name, strlen(name) + 1);
				uint8_t type = sqlite3_column_type(statement, i);
				_tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, &type, sizeof(type));
				switch (type)
				{
				case SQLITE_INTEGER:
					{
						int64_t value = sqlite3_column_int64(statement, i);
						_tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, &value, sizeof(value));
					}
					break;
				case SQLITE_FLOAT:
					{
						double value = sqlite3_column_double(statement, i);
						_tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, &value, sizeof(value));
					}
					break;
				case SQLITE_TEXT:
					{
						size_t bytes = sqlite3_column_bytes(statement, i);
						_tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, &bytes, sizeof(bytes));
						_tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, sqlite3_column_text(statement, i), bytes);
					}
					break;
				case SQLITE_BLOB:
					{
						size_t bytes = sqlite3_column_bytes(statement, i);
						_tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, &bytes, sizeof(bytes));
						_tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, sqlite3_column_blob(statement, i), bytes);
					}
					break;
				case SQLITE_NULL:
					break;
				default:
					abort();
				}
			}
		}
		sql_work->result = r;
		if (r == SQLITE_MISUSE)
		{
			sql_work->error = tf_strdup(sqlite3_errstr(sql_work->result));
		}
		else if (r != SQLITE_OK && r != SQLITE_DONE)
		{
			if (sqlite3_is_interrupted(db))
			{
				sql_work->error = tf_strdup("Timed out");
			}
			else
			{
				sql_work->error = tf_strdup(sqlite3_errmsg(db));
			}
		}
		_tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, &(uint8_t[]) { 0 }, 1);
		sqlite3_finalize(statement);
	}
	else if (sql_work->result != SQLITE_OK)
	{
		sql_work->error = tf_strdup(sqlite3_errmsg(db));
	}
	else
	{
		sql_work->result = SQLITE_ERROR;
		sql_work->error = tf_strdup("Statement not prepared");
	}
	uv_mutex_lock(&sql_work->lock);
	sql_work->db = NULL;
	uv_mutex_unlock(&sql_work->lock);
	tf_ssb_release_db_reader(ssb, db);
}

static void _tf_ssb_sqlAsync_handle_close(uv_handle_t* handle)
{
	sql_work_t* work = handle->data;
	handle->data = NULL;
	if (!work->async.data && !work->timeout.data)
	{
		tf_free(work);
	}
}

static void _tf_ssb_sqlAsync_destroy(sql_work_t* work)
{
	tf_free(work->binds);
	tf_free(work->error);
	if (work->rows)
	{
		tf_free(work->rows);
	}
	uv_mutex_destroy(&work->lock);
	uv_close((uv_handle_t*)&work->timeout, _tf_ssb_sqlAsync_handle_close);
	uv_close((uv_handle_t*)&work->async, _tf_ssb_sqlAsync_handle_close);
}

static void _tf_ssb_sqlAsync_after_work(tf_ssb_t* ssb, int status, void* user_data)
{
	sql_work_t* sql_work = user_data;
	JSContext* context = tf_ssb_get_context(ssb);
	uint8_t* p = sql_work->rows;
	JSValue result = JS_UNDEFINED;
	while (p < sql_work->rows + sql_work->rows_count && JS_IsUndefined(result))
	{
		if (*p++ == 'r')
		{
			JSValue row = JS_NewObject(context);

			while (*p == 'c' && JS_IsUndefined(result))
			{
				p++;
				const char* column_name = (const char*)p;
				size_t length = strlen((char*)p);
				p += length + 1;

				switch (*p++)
				{
				case SQLITE_INTEGER:
					{
						int64_t value = 0;
						memcpy(&value, p, sizeof(value));
						JS_SetPropertyStr(context, row, column_name, JS_NewInt64(context, value));
						p += sizeof(value);
					}
					break;
				case SQLITE_FLOAT:
					{
						double value = 0.0;
						memcpy(&value, p, sizeof(value));
						JS_SetPropertyStr(context, row, column_name, JS_NewFloat64(context, value));
						p += sizeof(value);
					}
					break;
				case SQLITE_TEXT:
				case SQLITE_BLOB:
					{
						size_t length = 0;
						memcpy(&length, p, sizeof(length));
						p += sizeof(length);
						JS_SetPropertyStr(context, row, column_name, JS_NewStringLen(context, (const char*)p, length));
						p += length;
					}
					break;
				case SQLITE_NULL:
					JS_SetPropertyStr(context, row, column_name, JS_NULL);
					break;
				}
			}

			result = JS_Call(context, sql_work->callback, JS_UNDEFINED, 1, &row);
			if (!JS_IsException(result))
			{
				JS_FreeValue(context, result);
				result = JS_UNDEFINED;
			}
			JS_FreeValue(context, row);
		}
		else
		{
			break;
		}
	}

	if (!JS_IsUndefined(result))
	{
		bool is_exception = JS_IsException(result);
		if (is_exception)
		{
			JSValue exception = JS_GetException(context);
			JS_FreeValue(context, result);
			result = exception;
		}
		JSValue promise_result = JS_Call(context, sql_work->promise[is_exception ? 1 : 0], JS_UNDEFINED, 1, &result);
		tf_util_report_error(context, promise_result);
		JS_FreeValue(context, promise_result);
	}
	else if (sql_work->result == SQLITE_OK || sql_work->result == SQLITE_DONE)
	{
		result = JS_Call(context, sql_work->promise[0], JS_UNDEFINED, 0, NULL);
		tf_util_report_error(context, result);
	}
	else
	{
		JSValue error = JS_ThrowInternalError(context, "SQL Error %s: %s", sql_work->error, sql_work->query);
		JSValue exception = JS_GetException(context);
		result = JS_Call(context, sql_work->promise[1], JS_UNDEFINED, 1, &exception);
		tf_util_report_error(context, result);
		JS_FreeValue(context, exception);
		JS_FreeValue(context, error);
	}
	JS_FreeValue(context, result);
	JS_FreeValue(context, sql_work->promise[0]);
	JS_FreeValue(context, sql_work->promise[1]);
	JS_FreeValue(context, sql_work->callback);
	JS_FreeCString(context, sql_work->query);
	_tf_ssb_sqlAsync_destroy(sql_work);
}

static void _tf_ssb_sqlAsync_timeout(uv_timer_t* timer)
{
	sql_work_t* work = timer->data;
	uv_mutex_lock(&work->lock);
	if (work->db)
	{
		sqlite3_interrupt(work->db);
	}
	uv_mutex_unlock(&work->lock);
}

static void _tf_ssb_sqlAsync_start_timer(uv_async_t* async)
{
	sql_work_t* work = async->data;
	uv_timer_start(&work->timeout, _tf_ssb_sqlAsync_timeout, k_sql_async_timeout_ms, 0);
}

static JSValue _tf_ssb_sqlAsync(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{
	tf_ssb_t* ssb = JS_GetOpaque(data[0], _tf_ssb_classId);
	const char* query = JS_ToCString(context, argv[0]);
	sql_work_t* work = tf_malloc(sizeof(sql_work_t));
	*work = (sql_work_t)
	{
		.async =
		{
			.data = work,
		},
		.timeout =
		{
			.data = work,
		},
		.ssb = ssb,
		.callback = JS_DupValue(context, argv[2]),
		.query = query,
	};
	uv_mutex_init(&work->lock);
	uv_async_init(tf_ssb_get_loop(ssb), &work->async, _tf_ssb_sqlAsync_start_timer);
	uv_timer_init(tf_ssb_get_loop(ssb), &work->timeout);
	JSValue result = JS_UNDEFINED;
	if (ssb)
	{
		result = JS_NewPromiseCapability(context, work->promise);
		int32_t length = tf_util_get_length(context, argv[1]);
		for (int i = 0; i < length; i++)
		{
			JSValue value = JS_GetPropertyUint32(context, argv[1], i);
			if (JS_IsNumber(value))
			{
				uint8_t type = SQLITE_INTEGER;
				int64_t number = 0;
				JS_ToInt64(context, &number, value);
				_tf_ssb_sql_append(&work->binds, &work->binds_count, &type, sizeof(type));
				_tf_ssb_sql_append(&work->binds, &work->binds_count, &number, sizeof(number));
			}
			else if (JS_IsBool(value))
			{
				uint8_t type = SQLITE_INTEGER;
				int64_t number = JS_ToBool(context, value) ? 1 : 0;
				_tf_ssb_sql_append(&work->binds, &work->binds_count, &type, sizeof(type));
				_tf_ssb_sql_append(&work->binds, &work->binds_count, &number, sizeof(number));
			}
			else if (JS_IsNull(value))
			{
				uint8_t type = SQLITE_NULL;
				_tf_ssb_sql_append(&work->binds, &work->binds_count, &type, sizeof(type));
			}
			else
			{
				uint8_t type = SQLITE_TEXT;
				size_t length = 0;
				const char* string = JS_ToCStringLen(context, &length, value);
				if (!string)
				{
					string = "";
				}
				_tf_ssb_sql_append(&work->binds, &work->binds_count, &type, sizeof(type));
				_tf_ssb_sql_append(&work->binds, &work->binds_count, &length, sizeof(length));
				_tf_ssb_sql_append(&work->binds, &work->binds_count, string, length);
				JS_FreeCString(context, string);
			}
			JS_FreeValue(context, value);
		}
		tf_ssb_run_work(ssb, _tf_ssb_sqlAsync_work, _tf_ssb_sqlAsync_after_work, work);
	}
	return result;
}

typedef struct _message_store_t
{
	JSContext* context;
	JSValue promise[2];
} message_store_t;

static void _tf_ssb_message_store_callback(const char* id, bool verified, bool is_new, void* user_data)
{
	message_store_t* store = user_data;
	JSValue result = JS_Call(store->context, id ? store->promise[0] : store->promise[1], JS_UNDEFINED, 0, NULL);
	tf_util_report_error(store->context, result);
	JS_FreeValue(store->context, result);
	JS_FreeValue(store->context, store->promise[0]);
	JS_FreeValue(store->context, store->promise[1]);
	tf_free(store);
}

static JSValue _tf_ssb_storeMessage(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{
	tf_ssb_t* ssb = JS_GetOpaque(data[0], _tf_ssb_classId);
	message_store_t* store = tf_malloc(sizeof(message_store_t));
	*store = (message_store_t) { .context = context };
	JSValue result = JS_NewPromiseCapability(context, store->promise);
	tf_ssb_verify_strip_and_store_message(ssb, argv[0], _tf_ssb_message_store_callback, store);
	return result;
}

typedef struct _broadcasts_t
{
	JSContext* context;
	JSValue array;
	int length;
} broadcasts_t;

static void _tf_ssb_broadcasts_visit(
	const char* host, const struct sockaddr_in* addr, tf_ssb_broadcast_origin_t origin, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data)
{
	broadcasts_t* broadcasts = user_data;
	JSValue entry = JS_NewObject(broadcasts->context);
	char pubkey[k_id_base64_len];
	tf_ssb_id_bin_to_str(pubkey, sizeof(pubkey), pub);
	switch (origin)
	{
	case k_tf_ssb_broadcast_origin_discovery:
		JS_SetPropertyStr(broadcasts->context, entry, "origin", JS_NewString(broadcasts->context, "discovery"));
		break;
	case k_tf_ssb_broadcast_origin_room:
		JS_SetPropertyStr(broadcasts->context, entry, "origin", JS_NewString(broadcasts->context, "room"));
		break;
	case k_tf_ssb_broadcast_origin_peer_exchange:
		JS_SetPropertyStr(broadcasts->context, entry, "origin", JS_NewString(broadcasts->context, "peer_exchange"));
		break;
	}
	if (tunnel)
	{
		JS_SetPropertyStr(broadcasts->context, entry, "tunnel", JS_DupValue(broadcasts->context, tf_ssb_connection_get_object(tunnel)));
	}
	else
	{
		JS_SetPropertyStr(broadcasts->context, entry, "address", JS_NewString(broadcasts->context, host));
		JS_SetPropertyStr(broadcasts->context, entry, "port", JS_NewInt32(broadcasts->context, ntohs(addr->sin_port)));
	}
	JS_SetPropertyStr(broadcasts->context, entry, "pubkey", JS_NewString(broadcasts->context, pubkey));
	JS_SetPropertyUint32(broadcasts->context, broadcasts->array, broadcasts->length++, entry);
}

static JSValue _tf_ssb_getBroadcasts(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{
	JSValue result = JS_UNDEFINED;
	tf_ssb_t* ssb = JS_GetOpaque(data[0], _tf_ssb_classId);
	if (ssb)
	{
		result = JS_NewArray(context);
		broadcasts_t broadcasts = {
			.context = context,
			.array = result,
			.length = 0,
		};
		tf_ssb_visit_broadcasts(ssb, _tf_ssb_broadcasts_visit, &broadcasts);
	}
	return result;
}

typedef struct _connect_t
{
	JSContext* context;
	JSValue promise[2];
} connect_t;

static void _tf_ssb_connect_callback(tf_ssb_connection_t* connection, const char* reason, void* user_data)
{
	connect_t* connect = user_data;
	JSContext* context = connect->context;
	JSValue arg = connection ? JS_UNDEFINED : JS_NewString(context, reason);
	JSValue result = JS_Call(context, connection ? connect->promise[0] : connect->promise[1], JS_UNDEFINED, connection ? 0 : 1, &arg);
	tf_util_report_error(context, result);
	JS_FreeValue(context, result);
	JS_FreeValue(context, connect->promise[0]);
	JS_FreeValue(context, connect->promise[1]);
	JS_FreeValue(context, arg);
	tf_free(connect);
}

static JSValue _tf_ssb_connect(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{
	JSValue result = JS_UNDEFINED;
	JSValue args = argv[0];
	tf_ssb_t* ssb = JS_GetOpaque(data[0], _tf_ssb_classId);
	if (ssb)
	{
		connect_t* connect = tf_malloc(sizeof(connect_t));
		*connect = (connect_t) { .context = context };
		result = JS_NewPromiseCapability(context, connect->promise);
		if (JS_IsString(args))
		{
			const char* address_str = JS_ToCString(context, args);
			tf_printf("Connecting to %s\n", address_str);
			tf_ssb_connect_str(ssb, address_str, 0, _tf_ssb_connect_callback, connect);
			JS_FreeCString(context, address_str);
		}
		else
		{
			JSValue address = JS_GetPropertyStr(context, args, "address");
			JSValue port = JS_GetPropertyStr(context, args, "port");
			JSValue pubkey = JS_GetPropertyStr(context, args, "pubkey");
			const char* address_str = JS_ToCString(context, address);
			int32_t port_int = 0;
			JS_ToInt32(context, &port_int, port);
			const char* pubkey_str = JS_ToCString(context, pubkey);
			if (pubkey_str)
			{
				tf_printf("Connecting to %s:%d\n", address_str, port_int);
				uint8_t pubkey_bin[k_id_bin_len];
				tf_ssb_id_str_to_bin(pubkey_bin, pubkey_str);
				tf_ssb_connect(ssb, address_str, port_int, pubkey_bin, 0, _tf_ssb_connect_callback, connect);
			}
			else
			{
				_tf_ssb_connect_callback(NULL, "Not connecting to null.", connect);
			}
			JS_FreeCString(context, pubkey_str);
			JS_FreeCString(context, address_str);
			JS_FreeValue(context, address);
			JS_FreeValue(context, port);
			JS_FreeValue(context, pubkey);
		}
	}
	return result;
}

typedef struct _forget_stored_connection_t
{
	const char* address;
	int32_t port;
	const char* pubkey;
	JSValue promise[2];
} forget_stored_connection_t;

static void _tf_ssb_forget_stored_connection_work(tf_ssb_t* ssb, void* user_data)
{
	forget_stored_connection_t* work = user_data;
	if (work->pubkey)
	{
		tf_ssb_db_forget_stored_connection(ssb, work->address, work->port, work->pubkey);
	}
}

static void _tf_ssb_forget_stored_connection_after_work(tf_ssb_t* ssb, int status, void* user_data)
{
	forget_stored_connection_t* work = user_data;
	JSContext* context = tf_ssb_get_context(ssb);
	JS_FreeCString(context, work->pubkey);
	JS_FreeCString(context, work->address);
	JSValue result = JS_Call(context, work->promise[0], JS_UNDEFINED, 0, NULL);
	tf_util_report_error(context, result);
	JS_FreeValue(context, result);
	JS_FreeValue(context, work->promise[0]);
	JS_FreeValue(context, work->promise[1]);
	tf_free(work);
}

static JSValue _tf_ssb_forgetStoredConnection(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{
	JSValue result = JS_UNDEFINED;
	JSValue args = argv[0];
	tf_ssb_t* ssb = JS_GetOpaque(data[0], _tf_ssb_classId);
	if (ssb)
	{
		JSValue address = JS_GetPropertyStr(context, args, "address");
		JSValue port = JS_GetPropertyStr(context, args, "port");
		JSValue pubkey = JS_GetPropertyStr(context, args, "pubkey");
		const char* address_str = JS_ToCString(context, address);
		int32_t port_int = 0;
		JS_ToInt32(context, &port_int, port);
		const char* pubkey_str = JS_ToCString(context, pubkey);

		forget_stored_connection_t* work = tf_malloc(sizeof(forget_stored_connection_t));
		*work = (forget_stored_connection_t) {
			.address = address_str,
			.port = port_int,
			.pubkey = pubkey_str,
		};
		result = JS_NewPromiseCapability(context, work->promise);
		JS_FreeValue(context, address);
		JS_FreeValue(context, port);
		JS_FreeValue(context, pubkey);
		tf_ssb_run_work(ssb, _tf_ssb_forget_stored_connection_work, _tf_ssb_forget_stored_connection_after_work, work);
	}
	return result;
}

typedef struct _blob_stored_t
{
	uv_timer_t timer;

	tf_ssb_t* ssb;
	JSContext* context;
	JSValue callback;

	char** ids;
	int ids_count;
} blob_stored_t;

static void _tf_ssb_on_blob_stored_timer(uv_timer_t* timer)
{
	blob_stored_t* blob_stored = timer->data;
	JSContext* context = blob_stored->context;
	JSValue array = JS_NewArray(context);
	for (int i = 0; i < blob_stored->ids_count; i++)
	{
		JS_SetPropertyUint32(context, array, i, JS_NewString(context, blob_stored->ids[i]));
		tf_free(blob_stored->ids[i]);
	}
	blob_stored->ids_count = 0;

	JSValue response = JS_Call(context, blob_stored->callback, JS_UNDEFINED, 1, &array);
	if (tf_util_report_error(context, response))
	{
		tf_ssb_remove_blob_stored_callback(blob_stored->ssb, _tf_ssb_on_blob_stored_callback, JS_VALUE_GET_PTR(blob_stored->callback));
	}
	JS_FreeValue(context, response);
	JS_FreeValue(context, array);
}

static void _tf_ssb_on_blob_stored_callback(tf_ssb_t* ssb, const char* id, void* user_data)
{
	blob_stored_t* blob_stored = user_data;
	blob_stored->ids = tf_resize_vec(blob_stored->ids, sizeof(char*) * (blob_stored->ids_count + 1));
	blob_stored->ids[blob_stored->ids_count++] = tf_strdup(id);

	if (uv_timer_get_due_in(&blob_stored->timer) == 0)
	{
		uv_timer_start(&blob_stored->timer, _tf_ssb_on_blob_stored_timer, 500, 0);
	}
}

static JSValue _tf_ssb_createTunnel(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{
	tf_ssb_t* ssb = JS_GetOpaque(data[0], _tf_ssb_classId);
	const char* portal_id = JS_ToCString(context, argv[0]);
	const char* target_id = JS_ToCString(context, argv[1]);

	bool result = portal_id && target_id && tf_ssb_tunnel_create(ssb, portal_id, target_id, 0);

	JS_FreeCString(context, target_id);
	JS_FreeCString(context, portal_id);
	return result ? JS_TRUE : JS_FALSE;
}

typedef struct _following_t
{
	JSContext* context;
	JSValue promise[2];

	tf_ssb_following_t* out_following;

	int depth;
	int ids_count;
	const char* ids[];
} following_t;

static void _tf_ssb_following_work(tf_ssb_t* ssb, void* user_data)
{
	following_t* following = user_data;
	following->out_following = tf_ssb_db_following_deep(ssb, following->ids, following->ids_count, following->depth, false);
}

static void _tf_ssb_following_after_work(tf_ssb_t* ssb, int status, void* user_data)
{
	following_t* following = user_data;
	JSContext* context = following->context;
	if (status == 0)
	{
		JSValue object = JS_NewObject(context);
		for (int i = 0; *following->out_following[i].id; i++)
		{
			JSValue entry = JS_NewObject(context);
			JS_SetPropertyStr(context, entry, "of", JS_NewInt32(context, following->out_following[i].following_count));
			JS_SetPropertyStr(context, entry, "ob", JS_NewInt32(context, following->out_following[i].blocking_count));
			JS_SetPropertyStr(context, entry, "if", JS_NewInt32(context, following->out_following[i].followed_by_count));
			JS_SetPropertyStr(context, entry, "ib", JS_NewInt32(context, following->out_following[i].blocked_by_count));
			JS_SetPropertyStr(context, entry, "d", JS_NewInt32(context, following->out_following[i].depth));
			JS_SetPropertyStr(context, object, following->out_following[i].id, entry);
		}
		JSValue result = JS_Call(context, following->promise[0], JS_UNDEFINED, 1, &object);
		tf_util_report_error(context, result);
		JS_FreeValue(context, result);
		JS_FreeValue(context, object);
	}
	else
	{
		char buffer[256];
		uv_strerror_r(status, buffer, sizeof(buffer));
		JSValue message = JS_NewString(context, buffer);
		JSValue result = JS_Call(context, following->promise[1], JS_UNDEFINED, 1, &message);
		tf_util_report_error(context, result);
		JS_FreeValue(context, result);
		JS_FreeValue(context, message);
	}

	JS_FreeValue(context, following->promise[0]);
	JS_FreeValue(context, following->promise[1]);

	for (int i = 0; i < following->ids_count; i++)
	{
		tf_free((void*)following->ids[i]);
	}
	tf_free(following->out_following);
	tf_free(following);
}

static JSValue _tf_ssb_following(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{
	tf_ssb_t* ssb = JS_GetOpaque(data[0], _tf_ssb_classId);
	int ids_count = tf_util_get_length(context, argv[0]);

	following_t* following = tf_malloc(sizeof(following_t) + sizeof(char*) * ids_count);
	*following = (following_t) {
		.context = context,
	};
	JS_ToInt32(context, &following->depth, argv[1]);

	JSValue result = JS_NewPromiseCapability(context, following->promise);

	for (int i = 0; i < ids_count; i++)
	{
		JSValue id_value = JS_GetPropertyUint32(context, argv[0], i);
		if (!JS_IsUndefined(id_value))
		{
			const char* id_string = JS_ToCString(context, id_value);
			following->ids[following->ids_count++] = tf_strdup(id_string);
			JS_FreeCString(context, id_string);
			JS_FreeValue(context, id_value);
		}
	}

	tf_ssb_run_work(ssb, _tf_ssb_following_work, _tf_ssb_following_after_work, following);
	return result;
}

static JSValue _tf_ssb_sync(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{
	tf_ssb_t* ssb = JS_GetOpaque(data[0], _tf_ssb_classId);
	tf_ssb_sync_start(ssb);
	return JS_UNDEFINED;
}

static JSValue _tf_ssb_port(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{
	tf_ssb_t* ssb = JS_GetOpaque(data[0], _tf_ssb_classId);
	return JS_NewInt32(context, tf_ssb_server_get_port(ssb));
}

JSValue tf_ssb_register(JSContext* context, tf_ssb_t* ssb)
{
	if (!_tf_ssb_classId)
	{
		JS_NewClassID(&_tf_ssb_classId);
		JSClassDef def = {
			.class_name = "ssb",
		};
		if (JS_NewClass(JS_GetRuntime(context), _tf_ssb_classId, &def) != 0)
		{
			fprintf(stderr, "Failed to register ssb.\n");
		}
	}

	JSValue object = JS_NewObjectClass(context, _tf_ssb_classId);
	JS_SetOpaque(object, ssb);

	/* Does not require an identity. */
	JS_SetPropertyStr(context, object, "getServerIdentity", JS_NewCFunctionData(context, _tf_ssb_getServerIdentity, 0, 0, 1, &object));
	JS_SetPropertyStr(context, object, "blobGet", JS_NewCFunctionData(context, _tf_ssb_blobGet, 1, 0, 1, &object));
	JS_SetPropertyStr(context, object, "blobHas", JS_NewCFunctionData(context, _tf_ssb_blobHas, 1, 0, 1, &object));
	JS_SetPropertyStr(context, object, "connections", JS_NewCFunctionData(context, _tf_ssb_connections, 0, 0, 1, &object));
	JS_SetPropertyStr(context, object, "storedConnections", JS_NewCFunctionData(context, _tf_ssb_storedConnections, 0, 0, 1, &object));
	JS_SetPropertyStr(context, object, "getConnection", JS_NewCFunctionData(context, _tf_ssb_getConnection, 1, 0, 1, &object));
	JS_SetPropertyStr(context, object, "closeConnection", JS_NewCFunctionData(context, _tf_ssb_closeConnection, 1, 0, 1, &object));
	JS_SetPropertyStr(context, object, "forgetStoredConnection", JS_NewCFunctionData(context, _tf_ssb_forgetStoredConnection, 1, 0, 1, &object));
	JS_SetPropertyStr(context, object, "sqlAsync", JS_NewCFunctionData(context, _tf_ssb_sqlAsync, 3, 0, 1, &object));
	JS_SetPropertyStr(context, object, "getBroadcasts", JS_NewCFunctionData(context, _tf_ssb_getBroadcasts, 0, 0, 1, &object));
	JS_SetPropertyStr(context, object, "connect", JS_NewCFunctionData(context, _tf_ssb_connect, 1, 0, 1, &object));
	JS_SetPropertyStr(context, object, "createTunnel", JS_NewCFunctionData(context, _tf_ssb_createTunnel, 3, 0, 1, &object));
	JS_SetPropertyStr(context, object, "following", JS_NewCFunctionData(context, _tf_ssb_following, 2, 0, 1, &object));
	JS_SetPropertyStr(context, object, "sync", JS_NewCFunctionData(context, _tf_ssb_sync, 0, 0, 1, &object));
	JS_SetPropertyStr(context, object, "port", JS_NewCFunctionData(context, _tf_ssb_port, 0, 0, 1, &object));
	/* Write. */
	JS_SetPropertyStr(context, object, "storeMessage", JS_NewCFunctionData(context, _tf_ssb_storeMessage, 1, 0, 1, &object));
	JS_SetPropertyStr(context, object, "blobStore", JS_NewCFunctionData(context, _tf_ssb_blobStore, 1, 0, 1, &object));

	return object;
}
