text: const router = require('express').Router();
  const { Product, Category, Tag, ProductTag } = require('../../models');
  
  // The `/api/products` endpoint
  
  // get all products
  router.get('/', async (req, res) => {
    try {
      const productData = await Product.findAll({
        attributes: ['product_name', 'price', 'stock'],
        include: [{ model: Category }, { model: Tag, through: ProductTag }]
      })
      res.status(200).json(productData);
    }
    catch (err) {
      console.log(err)
      res.status(500).json(err)
    }
    // find all products
    // be sure to include its associated Category and Tag data
  });
  
  // get one product
  router.get('/:id', async (req, res) => {
    // find a single product by its `id`
    // be sure to include its associated Category and Tag data
    try {
      const productData = await Product.findByPk(req.params.id, {
        include: [{ model: Tag, through: ProductTag }]
      })
  
      if (!productData) {
        res.status(404).json({ message: 'No product data found with this id!' })
        return
      }
      res.status(200).json(productData);
    }
    catch (err) {
      console.log(err)
      res.status(500).json(err)
    }
  });
  
  // create new product
  router.post('/', async (req, res) => {
  
    try {
      const productData = await Product.create({
        product_name: req.body.product_name,
        price: req.body.price,
        stock: req.body.stock,
        tagIds: req.body.tagIds
      })
      res.status(200).json(productData)
    }
    catch (err) {
      console.log(err);
      res.status(500).json(err)
    }
    /* req.body should look like this...
      {
        product_name: "Basketball",
        price: 200.00,
        stock: 3,
        tagIds: [1, 2, 3, 4]
      }
    */
    Product.create(req.body)
      .then((product) => {
        // if there's product tags, we need to create pairings to bulk create in the ProductTag model
        if (req.body.tagIds.length) {
          const productTagIdArr = req.body.tagIds.map((tag_id) => {
            return {
              product_id: product.id,
              tag_id,
            };
          });
          return ProductTag.bulkCreate(productTagIdArr);
        }
        // if no product tags, just respond
        res.status(200).json(product);
      })
      .then((productTagIds) => res.status(200).json(productTagIds))
      .catch((err) => {
        console.log(err);
        res.status(400).json(err);
      });
  });
  
  // update product
  router.put('/:id', async (req, res) => {
    // update product data
    try {
      const productData = await Product.update(req.body,
        {
          where: { id: req.params.id }
        })
      // find all associated tags from ProductTag
      const productTags = await ProductTag.findAll({ where: { product_id: req.params.id } });
      // get list of current tag_ids
      const productTagIds = productTags.map(({ tag_id }) => tag_id);
      // create filtered list of new tag_ids
      const newProductTags = req.body.tagIds
        .filter((tag_id) => !productTagIds.includes(tag_id))
        .map((tag_id) => {
          return {
            product_id: req.params.id,
            tag_id,
          };
        });
      // figure out which ones to remove
      const productTagsToRemove = productTags
        .filter(({ tag_id }) => !req.body.tagIds.includes(tag_id))
        .map(({ id }) => id);
      // run both actions
      var productDestroy = await ProductTag.destroy({ where: { id: productTagsToRemove } })
      var productCreate = await ProductTag.bulkCreate(newProductTags)
      const finproductData = await Product.findByPk(req.params.id,{
        attributes: ['product_name', 'price', 'stock'],
        include: [{ model: Category }, { model: Tag, through: ProductTag }]
      })
      res.status(200).json(finproductData);
    } catch (err) {
      console.log(err);
      res.status(500).json(err)
    }
  });
  
  
  router.delete('/:id', async (req, res) => {
    // delete one product by its `id` value
    try {
      const productData = await Product.destroy({
        where: {
          id: req.params.id
        }
      })
      if (!productData) {
        res.status(404).json({ message: 'No product found with this id!' })
        return;
      }
      res.status(200).json(productData)
    }
    catch (err) {
      console.log(err);
      res.status(500).json(err)
    }
  });
  
  module.exports = router;
  