const { deleteUploadedFiles } = require('./file-handlers');
const fs = require('fs');
const path = require('path');

module.exports = (error, req, res, next) => {
	console.log(error);
	const status = error.statusCode || 500;
	const message = error.message;
	const data = error.data;
	deleteUploadedFiles(req);

	const logPath = path.resolve(__dirname, '../log.log');
	const logEntry = `[${new Date().toISOString()}] ${error.stack || error}\n ${error.message}\n`;
	fs.appendFile(logPath, logEntry, (err) => {
		if (err) console.error('Failed to write to log file:', err);
	});

	res.status(status).json({ success: false, message: message, data: data });
};
