text: const { User, Thought } = require("../models");
  
  const thoughtController = {
      // Get all thoughts
      getAllThoughts(req, res) {
          Thought.find({})
              .select("-__v")
              .then((dbThoughtData) => res.json(dbThoughtData))
              .catch(err => res.json(err));
      },
  
      // Get thought 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 => res.json(err));
      },
  
      // create a thought by a user
      createThought({ body }, res) {
          Thought.create(body)
              .then(({ _id }) => {
                  return User.findOneAndUpdate({
                      _id: body.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.id }, 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.json(err));
      },
  
      // delete thought by ID
      deleteThought({ params }, res) {
          Thought.findOneAndDelete({ _id: params.thoughtId })
              .then(dbThoughtData => {
                  if (!dbThoughtData) {
                      return res.status(404).json({ message: 'No thought found with this id.' });
                  }
                  return User.findOneAndUpdate({ username: dbThoughtData.username }, { $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 a reaction
      createReaction({ params, body }, res) {
          Thought.findOneAndUpdate({ _id: params.thoughtId }, { $push: { reactions: body } }, { new: true, runValidators: 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));
      },
  
      // Delete a reaction
      deleteReaction({ params }, res) {
          Thought.findOneAndUpdate({ _id: params.thoughtId }, { $pull: { reactions: { reactionId: params.reactionId } } }, { new: true })
              .then(dbUserData => res.json(dbUserData))
              .catch(err => res.json(err));
      }
  };
  
  module.exports = thoughtController;