const reviweFunction = require("../function/review");
const adminFunction = require("../function/admin");

const addReview = async (req, res) => {
  try {
    const review = await reviweFunction.addReview(req);
    if (review) {
      const avgRating = await reviweFunction.overAllShopReviews(req);
      const { shopId } = req.body;
      const data = {
        shopId,
        avgRating,
      };
      const update = await adminFunction.updateRatingInShop(data);
      return res.status(200).json({
        success: true,
        msg: "Review Added Successfully!",
        data: review,
      });
    } else {
      return res.status(200).json({
        success: false,
        msg: "Review is Not Added!",
      });
    }
  } catch (error) {
    console.log("Having Errors :", error);
    return res.status(403).json({
      success: false,
      msg: "Having Errors!",
      error: error.message,
    });
  }
};

const getReviewsByShopId = async (req, res) => {
  try {
    const reviews = await reviweFunction.getReviewsByShopId(req);
    return res.status(200).json({
      success: true,
      msg: "All Reviews On Shop",
      data: reviews,
    });
  } catch (error) {
    console.log("Having Errors :", error);
    return res.status(403).json({
      success: false,
      msg: "Having Errors!",
      error: error.message,
    });
  }
};

const getReviewsByProduct = async (req, res) => {
  try {
    const reviews = await reviweFunction.getReviewsByProduct(req);
    return res.status(200).json({
      success: true,
      msg: "All Reviews on This Product!",
      data: reviews,
    });
  } catch (error) {
    console.log("Having Errors :", error);
    return res.status(403).json({
      success: false,
      msg: "Having Errors!",
      error: error.message,
    });
  }
};

const overAllShopReviews = async (req, res) => {
  try {
    const rating = await reviweFunction.overAllShopReviews(req);
    return res.status(200).json({
      success: true,
      msg: "Shop Average Rating!",
      data: rating,
    });
  } catch (error) {
    console.log("Having Errors :", error);
    return res.status(403).json({
      success: false,
      msg: "Having Errors!",
      error: error.message,
    });
  }
};

const overAllProductReviews = async (req, res) => {
  try {
    const rating = await reviweFunction.overAllProductReviews(req);
    return res.status(200).json({
      success: true,
      msg: "Product Average Rating!",
      data: rating,
    });
  } catch (error) {
    console.log("Having Errors :", error);
    return res.status(403).json({
      success: false,
      msg: "Having Errors!",
      error: error.message,
    });
  }
};

const overAllReviews = async (req, res) => {
  try {
    const rating = await reviweFunction.overAllReviews(req);
    return res.status(200).json({
      success: true,
      msg: "AllOver Rating On the Basis of Products and Shop Reviews!",
      data: rating,
    });
  } catch (error) {
    console.log("Having Errors :", error);
    return res.status(403).json({
      success: false,
      msg: "Having Errors!",
      error: error.message,
    });
  }
};

const deleteReview = async (req, res) => {
  try {
    const review = await reviweFunction.deleteReview(req);
    return res.status(200).json({
      success: true,
      msg: "Review Deleted Successfully!",
    });
  } catch (error) {
    console.log("Having Errors :", error);
    return res.status(403).json({
      success: false,
      msg: "Having Errors!",
      error: error.message,
    });
  }
};

module.exports = {
  addReview,
  getReviewsByShopId,
  getReviewsByProduct,
  overAllShopReviews,
  overAllProductReviews,
  overAllReviews,
  deleteReview,
};
