text: const { Thoughts, User } = require("../models");
  
  const thoughtContoller = {
    // get all thoughts
    getAllThoughts(req, res) {
      Thoughts.find({})
        .select("-__v")
        .sort({ _id: -1 })
        .then((dbThoughtData) => res.json(dbThoughtData))
        .catch((err) => {
          console.log(err);
          res.sendStatus(400);
        });
    },
  // view a thought by thoughtid
    getThoughtById({ params }, res) {
      Thoughts.findOne({ _id: params.thoughtId })
        .populate({
          path: "reactions",
          select: "-__v",
        })
        .select("-__v")
        .sort({ _id: -1 })
        .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.sendStatus(400);
        });
    },
  // add a new thought
    addThought(req, res) {
      console.log(req.params);
      Thoughts.create(req.body)
        .then(({ _id }) => {
          return User.findOneAndUpdate(
            { _id: req.params.userId },
            { $push: { thoughts: _id } },
            { new: true, runValidators: true }
          );
        })
        .then((dbThoughtData) => {
          console.log(dbThoughtData);
          if (!dbThoughtData) {
            res.status(404).json({ message: "No thought found with this id!" });
            return;
          }
          res.json(dbThoughtData);
        })
        .catch((err) => res.json(err));
    },
  
  // update a thought
    updateThought({ params, body }, res) {
      Thoughts.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.json(err));
    },
  
    // remove a thought
    removeThought({ params }, res) {
      Thoughts.findOneAndDelete({ _id: params.thoughtId })
        .then((deletedThought) => res.json(deletedThought))
        .catch((err) => res.json(err));
    },
  
  // create a reaction stored in a single thought
    addReaction({ params, body }, res) {
      Thoughts.findOneAndUpdate(
        { _id: params.thoughtId },
        { $push: { reactions: 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));
    },
  // pull and remove a reaction by the reactionId
  removeReaction({ params }, res) {
    Thoughts.findOneAndUpdate(
      { _id: params.thoughtId },
      { $pull: { reactions: { reactionId: params.reactionId } } },
      { new: 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));
  },
  };
  
  module.exports = thoughtContoller;
  