text: import express from "express";
  import expressAsyncHandler from "express-async-handler";
  import Product from "../models/ProdcutModel";
  import { isAuth } from "../middlewares/isAuth";
  import { isAdmin } from "../middlewares/isAdmin";
  
  const ProductRouter = express.Router();
  
  ProductRouter.get(
    "/",
    expressAsyncHandler(async (req, res) => {
      const searchKeyword = req.query.searchKeyword
        ? {
            name: {
              $regex: req.query.searchKeyword,
              $options: "i",
            },
          }
        : {};
      const products = await Product.find({ ...searchKeyword });
      res.send(products);
    })
  );
  ProductRouter.get(
    "/:id",
    expressAsyncHandler(async (req, res) => {
      const product = await Product.findById(req.params.id);
      res.send(product);
    })
  );
  ProductRouter.post(
    "/",
    isAuth,
    isAdmin,
    expressAsyncHandler(async (req, res) => {
      try {
        const newProduct = new Product({
          name: "sample",
          description: "sample prodcuct here",
          category: "product",
          brand: "brand",
          image: "/images/product-1.jpg",
          price: "50",
        });
        const createdProduct = await newProduct.save();
  
        if (createdProduct)
          res
            .status(201)
            .send({ message: "New product created", product: createdProduct });
        else res.status(500).send({ message: "Error in creating new product" });
      } catch (error) {
        res.status(500).send({ message: error.message });
      }
    })
  );
  
  ProductRouter.post(
    "/:id/reviews",
    isAuth,
    expressAsyncHandler(async (req, res) => {
      const product = await Product.findById(req.params.id);
      if (product) {
        const review = {
          rating: req.body.rating,
          comment: req.body.comment,
          user: req.user._id,
          name: req.user.name,
        };
        product.reviews.push(review);
        product.rating =
          product.reviews.reduce((a, c) => c.rating + a, 0) /
          product.reviews.length;
        product.numReviews = product.reviews.length;
        const updatedProduct = await product.save();
        res.status(201).send({
          message: "Comment Created.",
          data: updatedProduct.reviews[updatedProduct.reviews.length - 1],
        });
      } else {
        throw Error("Product does not exist.");
      }
    })
  );
  
  
  ProductRouter.put(
    "/:id", isAuth,
    isAdmin,
    expressAsyncHandler(async (req, res) => {
      const product = await Product.findById(req.params.id);
      if (!product)
        res.status(404).send({ message: "product not found , Try again" });
      else {
        product.name = req.body.name || product.name;
        product.description = req.body.description || product.description;
        product.category = req.body.category || product.category;
        product.brand = req.body.brand || product.brand;
        product.image = req.body.image || product.image;
        product.price = req.body.price || product.price;
        product.countInStock = req.body.countInStock || product.countInStock;
  
  
        const updatedProduct = await product.save();
  
        if (updatedProduct) res.send({ message: "Product updated", product: updatedProduct })
        else res.status(500).send({message : "Error in updating the product"})
      }
    })
  );
  ProductRouter.delete("/:id", isAuth, isAdmin, expressAsyncHandler(async (req, res) =>
  {
    const product = await Product.findById(req.params.id)
    console.log(product)
      if (!product)
        res.status(404).send({ message: "product not found , Try again" });
      else
      {
        const deletedProduct = await product.remove()
        res.send({message : "Product deleted", product : deletedProduct})
        }
  }))
  export default ProductRouter;
  