const CmsSection = require("../../models/CmsSection");
const Faq = require("../../models/Faq");
const OtherInfo = require("../../models/OtherInfo");

exports.cmsController = {
  getContactDetails: async (req, res) => {
    try {
      const contactDetails = await OtherInfo.findOne({ type: "contact" });
      return res.status(200).json({
        success: true,
        msg: "Contact Details",
        data: { ...contactDetails.data },
      });
    } catch (err) {
      return res
        .status(500)
        .json({ success: false, msg: "Something went wrong!" });
    }
  },
  getPrivacyPolicy: async (req, res) => {
    try {
      const privacyPolicy = await OtherInfo.findOne({ type: "privacyPolicy" });
      return res.status(200).json({
        success: true,
        msg: "Privacy Policy",
        data: privacyPolicy.data,
      });
    } catch (err) {
      return res
        .status(500)
        .json({ success: false, msg: "Something went wrong!" });
    }
  },
  getTermsNConditions: async (req, res) => {
    try {
      const termsNConditions = await OtherInfo.findOne({
        type: "terms&Conditions",
      });
      return res.status(200).json({
        success: true,
        msg: "Term & Conditions",
        data: termsNConditions.data,
      });
    } catch (err) {
      return res
        .status(500)
        .json({ success: false, msg: "Something went wrong!" });
    }
  },
  getAllFaqs: async (req, res) => {
    try {
      const faqs = await Faq.find().select("_id heading paragraph");

      return res.status(200).json({
        success: true,
        msg: "Faq",
        data: faqs,
      });
    } catch (err) {
      console.log(err);
      return res
        .status(500)
        .json({ success: false, msg: "Something went wrong!" });
    }
  },
  getSections: async (req, res) => {
    try {
      const { page } = req.query;

      const sections = await CmsSection.find({ page });

      return res
        .status(200)
        .json({ success: true, msg: "Sections", data: sections });
    } catch (err) {
      return res
        .status(500)
        .json({ success: false, msg: "Something went wrong!" });
    }
  },
};
