import * as Hapi from "@hapi/hapi";
import hapiPino from "hapi-pino";
import Schmervice from "@hapipal/schmervice";
import WhatsappService from "./service.js";
import {
  RegisterBotRoute,
  UnverifyBotRoute,
  GetBotRoute,
  SendMessageRoute,
  ReceiveMessageRoute,
} from "./routes.js";

const server = Hapi.server({ port: 5000 });

const startServer = async () => {
  await server.register({ plugin: hapiPino });

  server.route(RegisterBotRoute);
  server.route(UnverifyBotRoute);
  server.route(GetBotRoute);
  server.route(SendMessageRoute);
  server.route(ReceiveMessageRoute);

  await server.register(Schmervice);
  server.registerService(WhatsappService);

  await server.start();

  return server;
};

const main = async () => {
  await startServer();
};

main().catch((err) => {
  console.error(err);
  process.exit(1);
});
