import { NextFunction, Request, Response, Router } from "express";

import { config } from "@/config";
import { checkUser } from "@/contexts/auth";
import { handleRequest } from "@/contexts/common/infrastructure/express";
import { BrevoMailService, LoggerMailService } from "@/contexts/common/infrastructure/nodemailer";
import { getDealerMiddleware } from "@/contexts/sales/infrastructure/express/middlewares/Dealer.middleware";
import { createSendIncidenceController, ISupportContext } from "@/contexts/support/infrastructure";

export const supportRouter = (appRouter: Router): void => {
  const supportRoutes: Router = Router({ mergeParams: true });

  supportRoutes.use((req: Request, res: Response, next: NextFunction) => {
    const context = res.locals["context"];
    res.locals["context"] = {
      ...context,
      services: {
        emailService: config.isProduction ? new BrevoMailService() : new LoggerMailService(),
      },
    } as ISupportContext;
    next();
  });

  supportRoutes.post(
    "/",
    checkUser,
    getDealerMiddleware,
    handleRequest(createSendIncidenceController)
  );

  appRouter.use("/support", supportRoutes);
};
