const express = require("express");
const { body } = require("express-validator");
const validation = require("../../../../middlewares/routeValidation");
const isAuth = require("../../../../middlewares/authorization");
const jsonParser = require("../../../../helpers/jsonParser");
const pagination = require("../../../../middlewares/pagination");

const {
  createUpdateSellerProductV2,
  getSellerProductByIdV2,
  addOrDeductItemBeginningBalance,
} = require("../../../../controllers/seller/v2/products");
const { SELLER_PRODUCTS_MODULE } = require("../../../../helpers/common");
const checkPermissionMiddleware = require("../../../../middlewares/permission");

const router = express.Router();

router.post(
  "/",
  isAuth,
  (req, res, next) => {
    if (req.body && req.body.id) {
      return checkPermissionMiddleware(SELLER_PRODUCTS_MODULE, "edit")(
        req,
        res,
        next,
      );
    } else {
      return checkPermissionMiddleware(SELLER_PRODUCTS_MODULE, "add")(
        req,
        res,
        next,
      );
    }
  },
  createUpdateSellerProductV2,
);

router.get(
  "/",
  isAuth,
  checkPermissionMiddleware(SELLER_PRODUCTS_MODULE, "view"),
  getSellerProductByIdV2,
);

router.post(
  "/update-qty",
  isAuth,
  checkPermissionMiddleware(SELLER_PRODUCTS_MODULE, "edit"),
  addOrDeductItemBeginningBalance,
);

module.exports = router;
