import { checkUser } from "@/contexts/auth";
import { handleRequest } from "@/contexts/common/infrastructure/express";
import {
  createGetProfileController,
  createUpdateProfileController,
} from "@/contexts/profile/infrastructure";
import { createGetProfileLogoController } from "@/contexts/profile/infrastructure/express/controllers/getProfileLogo";
import { createUploadProfileLogoController } from "@/contexts/profile/infrastructure/express/controllers/uploadProfileLogo";
import { Router } from "express";
import { createMulterMiddleware } from "../Upload.middleware";

const uploadProfileLogo = createMulterMiddleware({
  uploadFolder: "uploads/dealer-logos",
  mimeTypes: ["image/jpeg", "image/png"],
  maxSize: 1024 * 1024 * 5, // 5Mb
});

export const profileRouter = (appRouter: Router) => {
  const profileRoutes: Router = Router({ mergeParams: true });

  profileRoutes.get("/", checkUser, handleRequest(createGetProfileController));
  profileRoutes.put("/", checkUser, handleRequest(createUpdateProfileController));
  profileRoutes.get("/logo", checkUser, handleRequest(createGetProfileLogoController));

  profileRoutes.post(
    "/logo",
    checkUser,
    uploadProfileLogo.single("logo"),
    handleRequest(createUploadProfileLogoController)
  );

  appRouter.use("/profile", profileRoutes);
};
