const mongoose = require("mongoose");
// const warehouse = mongoose.model("warehouse");

const WarehousepaginatedResults = (warehouse) => {
	return async (req, res, next) => {
		// console.log('in paginate function')
		const page = parseInt(req.query.page)
		const limit = parseInt(req.query.limit)

		const startIndex = (page - 1) * limit

		const results = {}
		let totalCount = await warehouse.countDocuments({}).exec()
		// console.log('total count :  ',totalCount)
		results.results = await warehouse.find({})
			.sort({ _id: -1 })
			.limit(limit)
			.skip(startIndex)
			.exec()

		res.paginatedResults = results
		res.totalCount = totalCount
		next()
	}
}
module.exports = WarehousepaginatedResults