const {
  searchService,
  sellersCompanyService,
} = require("../../helpers/search.service");

exports.search = async (req, res, next) => {
  try {
    const {
      keyword = "",
      priceMin = 0,
      priceMax = 0,
      category = "",
      seller = "",
      availability = "inStock", // Values: ["inStock", "outOfStock"]
      location = "all", // Values: ["near", "all"]
      longitude = 0,
      latitude = 0,
      page = 1,
      limit = 15,
      order = ""
    } = req.body;

    const searchResult = await searchService(req, {
      page,
      limit,
      keyword,
      priceMin,
      priceMax,
      category,
      seller,
      availability,
      location,
      longitude,
      latitude,
      order
    });

    if (searchResult.error) {
      return res
        .status(200)
        .json({ success: false, message: searchResult.error });
    }

    res.status(200).json({ success: true, ...searchResult });
  } catch (err) {
    next(err);
  }
};

exports.getAllSellers = async (req, res, next) => {
  try {
    const sellerCompanies = await sellersCompanyService();

    if (sellerCompanies.error) {
      return res
        .status(200)
        .json({ success: false, message: sellerCompanies.error });
    }
    return res.status(200).json({ success: true, ...sellerCompanies });
  } catch (err) {
    next(err);
  }
};
