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

// Get all drivers
router.get('/', driverController.getAll);

// Get drivers by status
router.get('/status/:status', driverController.getByStatus);

// Get drivers by ambulance ID
router.get('/ambulance/:ambulanceId', driverController.getByAmbulanceId);

// Get driver by user ID
router.get('/user/:userId', driverController.getByUserId);

// Get authenticated driver profile with assigned ambulance
router.get('/me', authenticateToken, driverController.getMyProfile);

// Get active requests for the authenticated driver
router.get('/me/active-requests', authenticateToken, driverController.getMyActiveRequests);

// Get recent requests for the authenticated driver
router.get('/me/recent-requests', authenticateToken, driverController.getMyRecentRequests);

// Get all trips/requests for the authenticated driver
router.get('/me/trips', authenticateToken, driverController.getMyTrips);

// Get aggregate trip stats for the authenticated driver
router.get('/me/trip-stats', authenticateToken, driverController.getMyTripStats);

// Confirm pickup for a trip (sets pickup_confirmed_at)
router.patch(
  '/me/trips/:requestId/confirm-pickup',
  authenticateToken,
  driverController.confirmPickup,
);

// Start trip (sets trip_started_at without changing status)
router.patch(
  '/me/trips/:requestId/start-trip',
  authenticateToken,
  driverController.startTrip,
);

// Self-assign a pending request
router.post(
  '/me/requests/:requestId/assign',
  authenticateToken,
  driverController.selfAssignRequest,
);

// Partially update authenticated driver info (user/driver fields)
router.patch('/me', authenticateToken, driverController.updateMyProfile);

// Get driver by ID
router.get('/:id', driverController.getById);

// Create new driver with user (creates both user and driver records)
router.post('/create-with-user', driverController.createWithUser);

// Create new driver (requires existing user_id)
router.post('/', driverController.create);

// Update driver
router.put('/:id', driverController.update);

// Update driver status
router.patch('/:id/status', driverController.updateStatus);

// Assign driver to ambulance
router.patch('/:id/assign-ambulance', driverController.assignAmbulance);

// Unassign driver from ambulance
router.patch('/:id/unassign-ambulance', driverController.unassignAmbulance);

// Delete driver
router.delete('/:id', driverController.delete);

module.exports = router;


