text: const { Thought } = require('../models');
  const User = require('../models/User');
  
  const thoughtController = {
      // get all thoughts
      getAllThoughts(req, res) {
          Thought.find({})
              // .populate('reactionId')
              .then(dbThoughtData => res.json(dbThoughtData))
              .catch(err => {
                  console.log(err);
                  res.status(400).json(err);
              });
      },
  
      // get one user by id
      getThoughtById({ params }, res) {
          Thought.findOne({ _id: params.thoughtId })
              .then(dbThoughtData => {
                  if (!dbThoughtData) {
                      res.status(404).json({ message: 'No thought found with this id!' });
                      return;
                  }
                  res.json(dbThoughtData);
              })
              .catch(err => {
                  console.log(err);
                  res.status(400).json(err);
              });
      },
  
      // add thought to user
      addThought({ params, body }, res) {
          console.log(body);
          Thought.create(body)
              .then(({ _id }) => {
                  return User.findOneAndUpdate(
                      { _id: params.userId },
                      { $push: { thoughts: _id } },
                      { new: true }
                  );
              })
              .then(dbUserData => {
                  if (!dbUserData) {
                      res.status(404).json({ message: 'No user found with this id!' });
                      return;
                  }
                  res.json(dbUserData);
              })
              .catch(err => res.json(err));
      },
  
      // update thought by id
      updateThought({ params, body }, res) {
          Thought.findOneAndUpdate({ _id: params.thoughtId }, body, { new: true, runValidators: true })
              .then(dbThoughtData => {
                  if (!dbThoughtData) {
                      res.status(404).json({ message: 'No thought found with this id!' });
                      return;
                  }
                  res.json(dbThoughtData);
              })
              .catch(err => res.status(400).json(err));
      },
  
      // remove thought
      removeThought({ params }, res) {
          Thought.findOneAndDelete({ _id: params.thoughtId })
              .then(deletedThought => {
                  if (!deletedThought) {
                      return res.status(404).json({ message: 'No thought with this id!' });
                  }
                  return User.findOneAndUpdate(
                      { _id: params.userId },
                      { $pull: { thoughts: params.thoughtId } },
                      { new: true }
                  );
              })
              .then(dbUserData => {
                  if (!dbUserData) {
                      res.status(404).json({ message: 'No user found with this id!' });
                      return;
                  }
                  res.json(dbUserData);
              })
              .catch(err => res.json(err));
      },
  
      // add reaction
      addReaction({ params, body }, res) {
          Thought.findOneAndUpdate(
              { _id: params.thoughtId },
              { $addToSet: { reactions: body } },
              { new: true, runValidators: true }
          )
              .then(dbThoughtData => {
                  if (!dbThoughtData) {
                      return res.status(404).json({ message: 'No thought found with this id!' });
                  }
                  res.json(dbThoughtData);
              })
              .catch(err => res.json(err));
      },
  
      // remove reaction
      removeReaction({ params }, res) {
          console.log(params.thoughtId, params.reactionId);
          Thought.findOneAndUpdate(
              { _id: params.thoughtId },
              { $pull: { reactions: { reactionId: params.reactionId } } },
              { runValidators: true, new: true }
          )
              .then(dbUserData => res.json(dbUserData))
              .catch(err => res.json(err));
      }
  };
  
  module.exports = thoughtController;