import { Application } from "express";
import consola from "consola";
import { config } from "../config";
import NatsClient from "../nats/NatsClient";
import { warmAll } from "../nats/RunnerKV";
import databaseLoader from "./database";
import expressLoader from "./express";
import openapiLoader from "./openapi";

/**
 * Index Loader that executes the other loaders in the right order.
 * This basicly exists for abstraction and a overall better dev experience.
 */
export default async (app: Application) => {
    await databaseLoader();
    await NatsClient.connect();
    
    if (config.nats_prewarm) {
        consola.info("Prewarming NATS runner cache...");
        const startTime = Date.now();
        await warmAll();
        const duration = Date.now() - startTime;
        consola.success(`NATS runner cache prewarmed in ${duration}ms`);
    }
    
    await openapiLoader(app);
    await expressLoader(app);
    return app;
};
