const express = require('express');
const router = express.Router();

const ctrlTrips = require('../controllers/trips');
const ctrlAuth = require('../controllers/authentication');
const { auth } = require('../config/jwt');
const { validateTripQuery, validateTripBody } = require('../middleware/validate');

// PUBLIC
// GET collection — supports ?page, ?limit, ?sortBy, ?order
router.get('/trips', validateTripQuery, ctrlTrips.tripsList);

// GET single trip by code
router.get('/trips/:tripCode', ctrlTrips.tripsFindByCode);

// PROTECTED
// POST create new trip — validates full body before reaching controller
router.post('/trips', auth, validateTripBody(true), ctrlTrips.tripsAddTrip);

// PUT update trip by code — allows partial body updates
router.put('/trips/:tripCode', auth, validateTripBody(false), ctrlTrips.tripsUpdateTrip);

// DELETE remove trip by code
router.delete('/trips/:tripCode', auth, ctrlTrips.tripsDeleteTrip);

// AUTH
router.post('/register', ctrlAuth.register);
router.post('/login', ctrlAuth.login);

module.exports = router;