import multer from "multer";
import crypto from "crypto";
import path from "path";
import fs from "fs";
import response from "../response.js";

const memoryStorage = multer.memoryStorage();

const fileFilter = (req, file, cb) => {
  const ext = path.extname(file.originalname).toLowerCase();
  if (ext === ".png" || ext === ".jpg" || ext === ".jpeg") {
    cb(null, true);
  } else {
    cb(
      new Error(
        "Invalid file type, only .png, .jpg, and .jpeg files are allowed!"
      ),
      false
    );
  }
};

const upload = multer({
  storage: memoryStorage,
  fileFilter,
  limits: { fileSize: 5 * 1024 * 1024 },
}).fields([{ name: "THUMBNAIL", maxCount: 1 }]);

const handleUpload = (req, res, next) => {
  upload(req, res, (err) => {
    if (err) {
      return response(400, null, err.message, res);
    }

    const files = req.files;
    const THUMBNAIL = files?.THUMBNAIL ? files.THUMBNAIL[0] : null;

    try {
      let validFiles = true;
      let errorMessages = [];

      if (THUMBNAIL && THUMBNAIL.size > 5 * 1024 * 1024) {
        validFiles = false;
        THUMBNAIL.buffer = null;
        errorMessages.push("Thumbnail file exceeds the size limit of 5MB");
      }

      if (validFiles) {
        req.filesToSave = { THUMBNAIL };
        next();
      } else {
        clearFileBuffers({ THUMBNAIL });
        return response(400, null, errorMessages.join(", "), res);
      }
    } catch (error) {
      console.log(error);
      clearFileBuffers({ THUMBNAIL });
      return response(500, null, "Internal Server Error", res);
    }
  });
};

export const clearFileBuffers = (files) => {
  for (const file of Object.values(files)) {
    if (file && file.buffer) {
      file.buffer = null;
    }
  }
};

export const saveFileToDisk = (file, fieldName, idSection) => {
  const ext = path.extname(file.originalname);
  const hash = crypto
    .createHash("md5")
    .update(file.originalname + file.buffer.length.toString())
    .digest("hex");
  const filename = `${fieldName}-${idSection}-${hash}${ext}`;
  const folderPath = path.join("public/uploads/section");

  if (!fs.existsSync(folderPath)) {
    fs.mkdirSync(folderPath, { recursive: true });
  }

  const filepath = path.join(folderPath, filename);
  fs.writeFileSync(filepath, file.buffer);
  return filename;
};

export default handleUpload;
