import type * as Hapi from "@hapi/hapi";
import Joi from "joi";
import type { IAppConfig } from "../config.js";
import * as Services from "./services/index.js";
import * as Routes from "./routes/index.js";
import * as Plugins from "./plugins/index.js";

const AppPlugin = {
  name: "App",
  async register(
    server: Hapi.Server,
    options: { config: IAppConfig }
  ): Promise<void> {
    // declare our **run-time** plugin dependencies
    // these are runtime only deps, not registration time
    // ref: https://hapipal.com/best-practices/handling-plugin-dependencies
    server.dependency(["config", "hapi-pino"]);

    server.validator(Joi);
    await Plugins.register(server, options.config);
    await Services.register(server);
    await Routes.register(server);
  },
};

export default AppPlugin;
