import * as Glue from "@hapi/glue";
import * as Metamigo from "@digiresilience/metamigo-common";
import * as Blipp from "blipp";
import HapiBasic from "@hapi/basic";
import AppPlugin from "../app/index.js";
import type { IAppConfig } from "../config.js";

const build = async (config: IAppConfig): Promise<Glue.Manifest> => {
  const { port, address } = config.server;
  const metamigoPlugins = Metamigo.defaultPlugins(config);
  return {
    server: {
      port,
      address,
      debug: false, // We use pino not the built-in hapi logger
      routes: {
        validate: {
          failAction: Metamigo.validatingFailAction,
        },
      },
    },
    register: {
      plugins: [
        // Blipp prints the nicely formatted list of endpoints at app boot
        { plugin: Blipp },

        // load the metamigo base plugins
        ...metamigoPlugins,

        // basic authentication, required by hapi-nextauth
        { plugin: HapiBasic },

        // load our main app
        {
          plugin: AppPlugin,
          options: {
            config,
          },
        },
      ],
    },
  };
};

const Manifest = {
  build,
};

export default Manifest;
