const createUploader = require("../utils/fileUploader");

function optionalMultipart(uploadMiddleware) {
  return (req, res, next) => {
    if (req.is("multipart/form-data")) {
      return uploadMiddleware(req, res, next);
    }
    next();
  };
}

const userUpload = createUploader("user").single("file");
const vendorUpload = createUploader("vendor").single("file");
const vendorUploads = createUploader("vendor").fields([
  { name: "file", maxCount: 1 },
  { name: "aadhaarCardFront", maxCount: 1 },
  { name: "aadhaarCardBack", maxCount: 1 },
  { name: "panCard", maxCount: 1 },
  { name: "shopLogo", maxCount: 1 },
  { name: "shopImage", maxCount: 1 },
  { name: "shopBanner", maxCount: 1 },
]);
const adminUpload = createUploader("admin").single("file");
const deliveryUpload = createUploader("delivery").single("file");
const categoryUpload = createUploader("category").single("file");
const subCategoryUpload = createUploader("sub-category").single("file");
const bannerUpload = createUploader("banner").single("file");
const promotionUpload = createUploader("promotion").single("file");
const notificationUpload = createUploader("notification").single("file");

exports.optionalUserFile = optionalMultipart(userUpload);
exports.optionalVendorFile = optionalMultipart(vendorUpload);
exports.optionalVendorFiles = optionalMultipart(vendorUploads);
exports.optionalAdminFile = optionalMultipart(adminUpload);
exports.optionalDeliveryBoyFile = optionalMultipart(deliveryUpload);
exports.optionalCategoryFile = optionalMultipart(categoryUpload);
exports.optionalSubCategoryFile = optionalMultipart(subCategoryUpload);
exports.optionalBannerFile = optionalMultipart(bannerUpload);
exports.optionalPromotionFile = optionalMultipart(promotionUpload);
exports.optionalNotificationFile = optionalMultipart(notificationUpload);
