text: const { User, Thought } = require('../models');
  
  const userController = {
    // the functions will go in here as methods
      // get all users
      getAllUser(req, res) {
          User.find({})
            .populate({
              path: "thoughts", 
              select: "-__v"})
            .populate({
              path: "friends", 
              select: "-__v"})
            .select("-__v")
            .sort({ _id: -1 })
            .then(dbUserData => res.json(dbUserData))
            .catch(err => {
              console.log(err);
              res.status(400).json(err);
            });
        },
      
        // get one user by id
        getUserById({ params }, res) {
          User.findOne({ _id: params.id })
            .populate({
              path: "thoughts", 
              select: "-__v"})
            .populate({
              path: "friends", 
              select: "-__v"})
            .select("-__v")
            .then(dbUserData => {
              // If no user is found, send 404
              if (!dbUserData) {
                res.status(404).json({ message: 'No User found with this id!' });
                return;
              }
              res.json(dbUserData);
            })
            .catch(err => {
              console.log(err);
              res.status(400).json(err);
            });
        },
  
        // create User
        createUser({ body }, res) {
        User.create(body)
          .then(dbUserData => res.json(dbUserData))
          .catch(err => res.status(400).json(err));
        },
  
        // update user by id
        updateUser({ params, body }, res) {
        User.findOneAndUpdate({ _id: params.id }, body, { new: true })
          .then(dbUserData => {
            if (!dbUserData) {
              res.status(404).json({ message: 'No User found with this id!' });
              return;
            }
          res.json(dbUserData);
          })
          .catch(err => res.status(400).json(err));
        },
  
        //delete User
        // deleteUser({ params }, res) {
        // User.findOneAndDelete({ _id: params.id })
        //   .then(dbUserData => {
        //     if (!dbUserData) {
        //       res.status(404).json({ message: 'No User found with this id!' });
        //       return;
        //     }
        //     res.json(dbUserData);
        //   })
        //   .catch(err => res.status(400).json(err));
        // },
  
        //delete user and user associated thoughts
        deleteUser({ params, body }, res) {
          Thought.deleteMany({ username: body.username })
          .then(() => {
            User.findOneAndDelete({ _id: params.id })
              .then(dbUserData => {
                if (!dbUserData) {
                  res.status(404).json({ message: 'No User found with this id!' });
                  return;
                }
                res.json(dbUserData);
              })
          })
              .catch(err => res.status(400).json(err));
            },
  
        addFriend({ params }, res ) {
          User.findOneAndUpdate(
            { _id: params.userId}, 
            {$push: {friends: params.friendId}}, 
            {new: true})
  
            .then(dbUserData => {
              if(!dbUserData) {
                  res.status(404).json({message: 'No user found with this id!'});
                  return;
              }
              res.json(dbUserData)
          })
          .catch(err => res.status(400).json(err));
        },
  
        removeFriend({ params }, res) {
          User.findOneAndUpdate(
            {_id: params.userId},
            {$pull: {friends: params.friendId}},
            {new: true}
          )
          .then(dbUserData => {
            if(!dbUserData) {
                res.status(404).json({message: 'No user found with this id!'});
                return;
            }
            res.json(dbUserData)
        })
        .catch(err => res.status(400).json(err));
  
        }
  
      
  };
  
  module.exports = userController;