const express = require('express');
const router = express.Router();
const ambulanceRequestController = require('../controllers/ambulanceRequestController');
const authenticateToken = require('../middleware/authMiddleware');
const requireAdmin = require('../middleware/adminMiddleware');

// Get authenticated user's own ambulance requests (requires Bearer token)
router.get('/my-requests', authenticateToken, ambulanceRequestController.getMyRequests);

// Get authenticated user's most recent pending or dispatched request (requires Bearer token)
router.get('/my-recent-request', authenticateToken, ambulanceRequestController.getMyRecentRequest);

// Cancel authenticated user's request (only if status is pending, requires Bearer token)
router.post('/cancel', authenticateToken, ambulanceRequestController.cancelMyRequest);

// Get all ambulance requests
router.get('/', ambulanceRequestController.getAll);

// Get all ambulance requests with full client data
router.get('/with-clients', ambulanceRequestController.getAllWithClients);

// Get all pending requests (for drivers to self-assign)
router.get('/pending', authenticateToken, ambulanceRequestController.getPendingRequests);

// Get ambulance request by ID
router.get('/:id', ambulanceRequestController.getById);

// Create new ambulance request
router.post('/', ambulanceRequestController.create);

// Create new ambulance request by admin (sets created_by to admin user)
router.post('/admin', requireAdmin, ambulanceRequestController.createByAdmin);

// Update ambulance request
router.put('/:id', ambulanceRequestController.update);

// Delete ambulance request
router.delete('/:id', ambulanceRequestController.delete);

module.exports = router;

