import handy_http_transport;
import slf4d;
import slf4d.default_provider;
import scheduled;
import std.datetime;

import api_mapping;
import util.config;
import analytics;

void main() {
	const config = readConfig();
	Level logLevel = getConfiguredLoggingLevel(config);
	auto provider = new DefaultProvider(logLevel);
    configureLoggingProvider(provider);
	infoF!"Loaded app config: port = %d, webOrigin = %s"(config.port, config.webOrigin);

	// Start scheduled tasks in a separate thread:
	JobSchedule analyticsSchedule = new FixedIntervalSchedule(
		hours(1),
		Clock.currTime(UTC()) + seconds(10)
	);

	JobScheduler jobScheduler = new TaskPoolScheduler();
	jobScheduler.addJob(() {
		info("Computing account balance time series analytics for all users...");
		doForAllUserProfiles(&computeAccountBalanceTimeSeries);
		info("Done computing analytics!");
	}, analyticsSchedule);
	jobScheduler.start();

	Http1TransportConfig transportConfig = defaultConfig();
	transportConfig.port = config.port;
	HttpTransport transport = new TaskPoolHttp1Transport(mapApiHandlers(config.webOrigin), transportConfig);
	transport.start();
}
