const express = require('express');
const { body } = require('express-validator');
const validate = require('../middleware/validate');
const { authenticate, authorize } = require('../middleware/auth');
const BookingController = require('../controllers/BookingController');
const upload = require('../middleware/upload');

const router = express.Router();

router.post(
  '/',
  [
    body('scheduleId').isInt().withMessage('Schedule ID required'),
    body('customerName').notEmpty().withMessage('Customer name required'),
    body('customerEmail').isEmail().withMessage('Valid customer email required'),
    body('customerPhone').notEmpty().withMessage('Customer phone required'),
    body('numberOfPassengers').isInt({ min: 1 }).withMessage('At least 1 passenger required'),
    body('fromStopId').optional().isInt().withMessage('From stop must be an integer'),
    body('toStopId').optional().isInt().withMessage('To stop must be an integer'),
    validate,
  ],
  BookingController.create
);

router.get('/', authenticate, authorize('admin', 'manager', 'staff'), BookingController.getAll);
router.get('/check-availability', BookingController.checkAvailability);
router.get('/:reference', BookingController.getByReference);
router.put('/:id/status', authenticate, authorize('admin', 'manager'), BookingController.updateStatus);
router.put('/:id', authenticate, authorize('admin', 'manager'), BookingController.update);
router.delete('/:id', authenticate, BookingController.delete);
router.post('/:id/image', authenticate, authorize('admin', 'manager'), upload.single('image'), BookingController.uploadImage);

router.post('/:id/proof-of-payment', upload.single('proof'), BookingController.uploadPOP);
router.get('/:id/proof-of-payment/download', authenticate, authorize('admin', 'manager'), BookingController.downloadPOP);

module.exports = router;
