import cors from "@fastify/cors";
import fjwt from "@fastify/jwt";
import multipart from "@fastify/multipart";
import dotenv from "dotenv";
import Fastify from "fastify";
import {
  ZodTypeProvider,
  serializerCompiler,
  validatorCompiler,
} from "fastify-type-provider-zod";
import { adminAuth, auth } from "./middlewares/auth";
import { adminAlbumsRoutes } from "./modules-admin/albums/route";
import { adminAppointmentsRoutes } from "./modules-admin/appointments/route";
import { adminAuthRoutes } from "./modules-admin/auth/route";
import { adminCcisRoutes } from "./modules-admin/ccis/route";
import { adminCompaniesRoutes } from "./modules-admin/companies/route";
import { adminEditionsRoutes } from "./modules-admin/editions/route";
import { adminEventsRoutes } from "./modules-admin/events/route";
import { adminFairsRoutes } from "./modules-admin/fairs/route";
import { adminMinioRoutes } from "./modules-admin/minio/route";
import { adminOpportunitiesRoutes } from "./modules-admin/opportunities/route";
import { adminProfileRoutes } from "./modules-admin/profile/route";
import { adminSettingRoutes } from "./modules-admin/settings/route";
import { adminSubAdminsRoutes } from "./modules-admin/sub-admins/route";
import { adminTrainingsRoutes } from "./modules-admin/trainings/route";
import { adminUsersRoutes } from "./modules-admin/users/route";
import { adminVideosRoutes } from "./modules-admin/videos/route";
import { albumsRoutes } from "./modules/albums/route";
import { appointmentsRoutes } from "./modules/appointments/route";
import { authRoutes } from "./modules/auth/route";
import { chatRoutes } from "./modules/chat/route";
import { companyRoutes } from "./modules/company/route";
import { editionsRoutes } from "./modules/editions/route";
import { eventsRoutes } from "./modules/events/route";
import { fairsRoutes } from "./modules/fairs/route";
import { homeRoutes } from "./modules/home/route";
import { networkingRoutes } from "./modules/networking/route";
import { opportunitiesRoutes } from "./modules/opportunities/route";
import { profileRoutes } from "./modules/profile/route";
import { servicesRoutes } from "./modules/services/route";
import { settingRoutes } from "./modules/settings/route";
import { trainingsRoutes } from "./modules/trainings/route";
import { videosRoutes } from "./modules/videos/route";
import { MAX_FILE_SIZE } from "./utils/consts";
import { adminServicesRoutes } from "./modules-admin/services/route";
import { wpRoutes } from "./modules/wp/route";
import { adminWpRoutes } from "./modules-admin/wp/route";

import { adminElectedMembersRoutes } from "./modules-admin/elected-members/route";

dotenv.config({ path: ".env" });

const fastify = Fastify({
  logger: true,
  bodyLimit: 10 * 1024 * 1024 * 8,
}).withTypeProvider<ZodTypeProvider>();

// COMMENT OUT or REMOVE the CORS plugin
// fastify.register(cors, {
//   origin: "*",
//   methods: ["GET", "PUT", "POST", "DELETE"],
//   credentials: true,
// });
fastify.setValidatorCompiler(validatorCompiler);
fastify.setSerializerCompiler(serializerCompiler);

// -------------------------------------------------------------------------
fastify.register(fjwt, { secret: process.env.SECRET! });

fastify.decorateRequest("jwt", null);
fastify.addHook("preHandler", async (req, res) => {
  req.jwt = fastify.jwt;
});
// -------------------------------------------------------------------------
fastify.decorate("auth", auth);
fastify.decorate("adminAuth", adminAuth);
// -------------------------------------------------------------------------

const listeners = ["SIGINT", "SIGTERM"];
listeners.forEach((signal) => {
  process.on(signal, async () => {
    await fastify.close();
    process.exit(0);
  });
});

// -------------------------------------------------------------------------
fastify.register(multipart, {
  attachFieldsToBody: true,
  limits: { fileSize: MAX_FILE_SIZE / 8 },
});
// -------------------------------------------------------------------------
fastify.register(settingRoutes, { prefix: "api/settings/" });
// -------------------------------------------------------------------------
fastify.register(authRoutes, { prefix: "api/" });
fastify.register(profileRoutes, { prefix: "api/profile/" });
fastify.register(homeRoutes, { prefix: "api/home/" });
fastify.register(companyRoutes, { prefix: "api/company/" });
fastify.register(networkingRoutes, { prefix: "api/networking/" });
fastify.register(chatRoutes, { prefix: "api/chat/" });
fastify.register(opportunitiesRoutes, { prefix: "api/opportunities/" });
fastify.register(appointmentsRoutes, { prefix: "api/appointments/" });
fastify.register(eventsRoutes, { prefix: "api/events/" });
fastify.register(albumsRoutes, { prefix: "api/albums/" });
fastify.register(videosRoutes, { prefix: "api/videos/" });
fastify.register(editionsRoutes, { prefix: "api/editions/" });
fastify.register(trainingsRoutes, { prefix: "api/trainings/" });
fastify.register(fairsRoutes, { prefix: "api/fairs/" });
fastify.register(servicesRoutes, { prefix: "api/services/" });
fastify.register(wpRoutes, { prefix: "api/wp/" });
// -------------------------------------------------------------------------
fastify.register(adminAuthRoutes, { prefix: "api/admin/auth/" });
fastify.register(adminProfileRoutes, { prefix: "api/admin/profile/" });
fastify.register(adminUsersRoutes, { prefix: "api/admin/users/" });
fastify.register(adminSubAdminsRoutes, { prefix: "api/admin/sub-admins/" });
fastify.register(adminCompaniesRoutes, { prefix: "api/admin/companies/" });
fastify.register(adminOpportunitiesRoutes, {
  prefix: "api/admin/opportunities/",
});
fastify.register(adminEventsRoutes, { prefix: "api/admin/events/" });
fastify.register(adminAppointmentsRoutes, {
  prefix: "api/admin/appointments/",
});
fastify.register(adminAlbumsRoutes, { prefix: "api/admin/albums/" });
fastify.register(adminVideosRoutes, { prefix: "api/admin/videos/" });
fastify.register(adminEditionsRoutes, { prefix: "api/admin/editions/" });
fastify.register(adminTrainingsRoutes, { prefix: "api/admin/trainings/" });
fastify.register(adminFairsRoutes, { prefix: "api/admin/fairs/" });
fastify.register(adminCcisRoutes, { prefix: "api/admin/ccis/" });
fastify.register(adminSettingRoutes, { prefix: "api/admin/settings/" });
fastify.register(adminMinioRoutes, { prefix: "api/admin/minio/" });
fastify.register(adminServicesRoutes, { prefix: "api/admin/services/" });
fastify.register(adminWpRoutes, { prefix: "api/admin/wp/" });
fastify.register(adminElectedMembersRoutes, {
  prefix: "api/admin/elected-members/",
});
// -----------------------------------------------------------------------

fastify.get('/api/health', async () => {
  return { status: 'ok', message: 'CCIS Connect API opérationnelle' };
});


export { fastify };
