text: const Tour = require('../models/tourModel');
  const User = require('../models/userModel');
  const Booking = require('../models/bookingModel');
  const catchAsync = require('../utils/catchAsync');
  const AppError = require('../utils/appError');
  
  exports.alerts = (req, res, next) => {
    const { alert } = req.query;
    if (alert === 'booking')
      res.locals.alert =
        "Your booking was successful! Please check your email for confirmation. If your booking doesn't show up immediatly, please come back later";
  
    next();
  };
  
  exports.getOverview = catchAsync(async (req, res, next) => {
    //  1. Get all tour data from our backend API collection
    const tours = await Tour.find();
    // 2. Build template
  
    // 3. Render the template using the tour data from the collection
    res.status(200).render('overview', {
      title: 'All tours',
      tours
    });
  });
  
  exports.getTour = catchAsync(async (req, res, next) => {
    // 1. Get the data, for the requested tour (includes reviews and tour guides)
    const tour = await Tour.findOne({ slug: req.params.slug }).populate({
      path: 'reviews',
      fields: 'review rating user'
    });
  
    if (!tour) {
      return next(new AppError('There is no tour with that name', 404));
    }
    // 2. Build templates
  
    // 3. Render template using data from step 1.
    res.status(200).render('tour', {
      title: `${tour.name} Tour`,
      tour
    });
  });
  
  exports.getLoginForm = (req, res) => {
    res.status(200).render('login', {
      title: 'Log into your account'
    });
  };
  
  exports.getSignupForm = (req, res) => {
    res.status(200).render('signup', {
      title: 'Sign up for an account'
    });
  };
  
  exports.getAccount = (req, res) => {
    res.status(200).render('account', {
      title: 'Your Account'
    });
  };
  
  exports.getMyTours = catchAsync(async (req, res, next) => {
    // 1. Find all bookingSchema
    const bookings = await Booking.find({ user: req.user.id });
    // 2. Find tours with the returned IDs
  
    const tourIDs = bookings.map(el => el.tour);
    const tours = await Tour.find({ _id: { $in: tourIDs } });
  
    res.status(200).render('overview', {
      title: 'My Tours',
      tours
    });
  });
  
  exports.updateUserData = catchAsync(async (req, res, next) => {
    const updatedUser = await User.findByIdAndUpdate(
      req.user.id,
      {
        name: req.body.name,
        email: req.body.email
      },
      {
        new: true,
        runValidators: true
      }
    );
  
    res.status(200).render('account', {
      title: 'Your Account',
      user: updatedUser
    });
  });
  
  exports.getSearchTour = catchAsync(async (req, res, next) => {
    const { searchTerm } = req.params;
    const searchTours = await Tour.find({
      $or: [
        { name: { $regex: `${searchTerm}`, $options: 'i' } },
        { description: { $regex: `${searchTerm}`, $options: 'i' } }
      ]
    }).populate({
      path: 'reviews',
      fields: 'review rating user'
    });
    if (!searchTours) {
      return next(new AppError('There is no tour with that name', 404));
    }
  
    // 3. Render the template using the tour data from the collection
    res.status(200).render('overview', {
      title: 'Search Result',
      tours: searchTours
    });
  });
  