import * as Metamigo from "@digiresilience/metamigo-common";
import { defState } from "@digiresilience/montar";
import Manifest from "./manifest.js";
import config, { IAppConfig } from "../config.js";

export const deployment = async (
  config: IAppConfig,
  start = false
): Promise<Metamigo.Server> => {
  // Build the manifest, which describes all the plugins needed for our application server
  const manifest = await Manifest.build(config);

  // Create the server and optionally start it
  const server = Metamigo.deployment(manifest, config, start);

  return server;
};

export const stopDeployment = async (server: Metamigo.Server): Promise<void> =>
  Metamigo.stopDeployment(server);

const server = defState("server", {
  start: () => deployment(config, true),
  stop: () => stopDeployment(server),
});

export default server;
