const { error, success } = require("../../handlers");
const { Feedback, User } = require("../../models");

const index = async (req, res) => {
  try {
    let propertyId = req.params?.propertyId,
      query = {};
    if (propertyId) {
      query["propertyId"] = propertyId;
    }
    let feedbacks = await Feedback.findAll({
      where: query,
      include: [
        {
          model: User,
          attributes: ["firstName", "lastName", "image"],
        },
      ],
      order: [["id", "desc"]],
    });

    return success(res, {
      msg: "Feedbacks listed successfully!!",
      data: feedbacks,
    });
  } catch (err) {
    return error(res, {
      msg: "Something went wrong!!",
    });
  }
};

const addFeedback = async (req, res) => {
  try {
    let payload = req.body;
    payload["userId"] = req?.user?.id;

    await Feedback.create(payload);
    return success(res, {
      msg: "Feedback added successfully!!",
      data: [],
    });
  } catch (err) {
    return error(res, {
      msg: "Something went wrong!!",
    });
  }
};

module.exports = {
  index,
  addFeedback,
};
