const multer  = require('multer')
const path = require('path');
const storage = multer.diskStorage({
    destination: function (req : any , file : any , cb : any ) {
      
      cb(null, "uploads/"); // Set the upload directory
    },
    filename: function (req : any , file : any , cb : any) {
      // Extract original extension
      const ext = path.extname(file.originalname);
      // Generate a unique filename and keep the extension
      const uniqueName = Date.now() + "-" + Math.round(Math.random() * 1E9) + ext;
      cb(null, uniqueName);
    },
  });
const upload = multer({  storage: storage  }).array('file', 12);
const { apiResponse} = require("../../core/response/response")
const { SUCCESS, REDIRECTION, CLIENT_ERROR, SERVER_ERROR } = require("../../core/response/statusCode")
const {ERROR_MSG, SUCCESS_MSG} = require("../../core/response/messages")
export {};




module.exports = function (req, res, next) {
    let lang = req.headers["accept-language"] || 'en'
    try {
        upload(req, res, function (err) {
            if (err) {  // This handles both MulterError and any other errors
                console.log(err)
                return apiResponse(res, true, [], ERROR_MSG[`SYSTEM-ERROR`], SERVER_ERROR.internalServerError, [], req);
            }
            next();
        })
    }catch(error){
        console.log(error)
        return apiResponse(res, true, [], ERROR_MSG[`SYSTEM-ERROR-${lang}`], SERVER_ERROR.internalServerError, [],req)
    }
}