const multer = require('multer');
const path = require('path');
const fs = require('fs');

// Pastikan folder storage/img ada
const storagePath = path.join(__dirname, '../../storage/img');
if (!fs.existsSync(storagePath)) {
    fs.mkdirSync(storagePath, { recursive: true });
}

const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, storagePath);
    },
    filename: (req, file, cb) => {
        const ext = path.extname(file.originalname);
        const filename = `${Date.now()}-${Math.round(Math.random() * 1E9)}${ext}`;
        cb(null, filename);
    }
});

const upload = multer({ storage });

module.exports = upload;
