const express = require('express');
const router = express.Router();
const multer = require('multer'); 
const path = require('path');
const { 
    getAllBookings, 
    updateBookingStatus,
    updatePaymentStatus,
    updateBookingDetails, 
    addBooking, 
    getBookingById,
    getInvoiceById,
    getDailyBookingStats,
    getTopVehicles
} = require('../controllers/bookingController');
const { auth, admin } = require('../middleware/authMiddleware');

const storage = multer.diskStorage({
    destination: (req, file, cb) => cb(null, path.join(__dirname, '../uploads')),
    filename: (req, file, cb) => {
        const prefix = file.fieldname === 'car_images' ? 'img' : 'vid';
        cb(null, `${prefix}_${Date.now()}${path.extname(file.originalname)}`);
    }
});

const upload = multer({ storage });

const multiUpload = upload.fields([
    { name: 'car_images', maxCount: 10 },
    { name: 'car_videos', maxCount: 5 }
]);

router.get('/stats/daily', auth, admin, getDailyBookingStats);
router.get('/stats/top-vehicles', auth, admin, getTopVehicles);
router.get('/', auth, admin, getAllBookings);
router.get('/:id', auth, admin, getBookingById);
router.patch('/:id/status', auth, admin, updateBookingStatus);
router.patch('/:id/payment', auth, admin, updatePaymentStatus);

router.post('/', auth, admin, multiUpload, addBooking);
router.get("/invoice/:id", getInvoiceById);
router.put('/:id', auth, admin, multiUpload, updateBookingDetails);

module.exports = router;