const express = require('express');
const router = express.Router();
const multer = require('multer');
const path = require('path');
const { addCustomer, searchCustomers, getAllCustomers, updateCustomer, getCountryMaster } = require('../controllers/customerController');
const { auth } = require('../middleware/authMiddleware');

// CHANGE THIS PART - Remove spaces and special characters
const storage = multer.diskStorage({
    destination: 'uploads/',
    filename: (req, file, cb) => {
        // Remove spaces and special characters, replace with underscore
        const cleanName = file.originalname.replace(/[^a-zA-Z0-9.]/g, '_');
        cb(null, Date.now() + '_' + cleanName);
    }
});
const upload = multer({ storage });

// Routes - No changes here
router.post('/', auth, upload.fields([
    { name: 'license_file', maxCount: 10 },
    { name: 'id_file', maxCount: 10 }
]), addCustomer);

router.put('/:id', auth, upload.fields([
    { name: 'license_file', maxCount: 10 },
    { name: 'id_file', maxCount: 10 }
]), updateCustomer);

router.get('/search', auth, searchCustomers); 
router.get('/', auth, getAllCustomers);
router.get('/countries', auth, getCountryMaster);

module.exports = router;

