import Fastify from "fastify";
import cors from "@fastify/cors";
import multipart from "@fastify/multipart";
import { config } from "./config.js";
import { authorize } from "./plugins/authz.js";
import { registerFormRoutes } from "./modules/forms/routes.js";
import { registerMasterDataRoutes } from "./modules/masterData/routes.js";
import { registerPocRoutes } from "./modules/poc/routes.js";

const app = Fastify({
  logger: process.env["NODE_ENV"] !== "production",
  disableRequestLogging: process.env["NODE_ENV"] === "production",
  ajv: { customOptions: { removeAdditional: false } },
});

await app.register(cors, { origin: "http://localhost:3000" });
await app.register(multipart, { limits: { fileSize: 25 * 1024 * 1024 } });

app.addHook("preHandler", authorize);

await registerFormRoutes(app);
await registerMasterDataRoutes(app);
await registerPocRoutes(app);

app.get("/health", async () => ({ ok: true }));

app.listen({ port: config.port, host: "0.0.0.0" }).catch((error) => {
  app.log.error(error);
  process.exit(1);
});
