import handy_httpd;
import handy_httpd.handlers.path_handler;
import std.process;

static import api_modules.auth;
static import api_modules.announcement;
static import api_modules.classroom_compliance.api;

void main() {
	string env = environment.get("TEACHER_TOOLS_API_ENV", "DEV");

	ServerConfig config;
	config.enableWebSockets = false;
	config.port = 8080;
	config.workerPoolSize = 3;

	config.defaultHeaders["Access-Control-Allow-Origin"] = "*";
	config.defaultHeaders["Access-Control-Allow-Methods"] = "*";
	config.defaultHeaders["Access-Control-Request-Method"] = "*";
	config.defaultHeaders["Access-Control-Allow-Headers"] = "Authorization, Content-Length, Content-Type, X-Admin-As-User";

	if (env == "PROD") {
		config.port = 8107;
		config.workerPoolSize = 5;
	}

	PathHandler handler = new PathHandler();
	handler.addMapping(Method.OPTIONS, "/api/**", &optionsEndpoint);
	if (env == "DEV") {
		handler.addMapping("/api/initialize-schema", &initializeSchemaEndpoint);
		handler.addMapping("/api/generate-sample-data", &sampleDataEndpoint);
	}
	api_modules.auth.registerApiEndpoints(handler);
	api_modules.announcement.registerApiEndpoints(handler);
	api_modules.classroom_compliance.api.registerApiEndpoints(handler);

	HttpServer server = new HttpServer(handler, config);
	server.start();
}

void optionsEndpoint(ref HttpRequestContext ctx) {
	ctx.response.status = HttpStatus.OK;
}

void initializeSchemaEndpoint(ref HttpRequestContext ctx) {
	import db : initializeSchema;
	initializeSchema();
}

void sampleDataEndpoint(ref HttpRequestContext ctx) {
	import sample_data : insertSampleData;
	insertSampleData();
}
