const express = require("express");
const router = express.Router();
const authorize = require("../../../../middlewares/authorize-sapps");
const clientCollabService = require("./clientCollab..service");

router.post("/create", authorize(), create);
router.post("/create-details", authorize(), createDetails);
router.post("/bulk-update-details", authorize(), bulkUpdateSubmit);
router.post("/reset-all-details", authorize(), resetDetails);
router.get("/", authorize(), getAll);
router.get("/filter", authorize(), filter);
router.get("/territory-period", authorize(), getByTerritoryPeriod);
router.get("/collab-by-id", authorize(), getByCollabId);
router.get("/option", authorize(), option);
router.get("/count", authorize(), count);
router.put("/:id", authorize(), update);
router.put("/enabled/:id", authorize(), enabled);
router.delete("/:id", authorize(), _delete);
router.delete("/details/:id", authorize(), deleteDetails);

module.exports = router;

function create(req, res, next) {
  clientCollabService
    .create(req.body)
    .then((result) => res.json(result))
    .catch(next);
}

function bulkUpdateSubmit(req, res, next) {
  clientCollabService
    .bulkUpdateSubmit(req.body)
    .then((result) => res.json(result))
    .catch(next);
}

function resetDetails(req, res, next) {
  clientCollabService
    .resetDetails(req.body)
    .then((result) => res.json(result))
    .catch(next);
}

function createDetails(req, res, next) {
  clientCollabService
    .createDetails(req.body)
    .then((result) => res.json(result))
    .catch(next);
}

function getAll(req, res, next) {
  clientCollabService
    .getAll(req.query)
    .then((result) => res.json(result))
    .catch(next);
}

function filter(req, res, next) {
  clientCollabService
    .filter(req.query)
    .then((result) => res.json(result))
    .catch(next);
}

function getByTerritoryPeriod(req, res, next) {
  clientCollabService
    .getByTerritoryPeriod(req.query)
    .then((result) => res.json(result))
    .catch(next);
}

function getByCollabId(req, res, next) {
  clientCollabService
    .getByCollabId(req.query)
    .then((result) => res.json(result))
    .catch(next);
}

function option(req, res, next) {
  clientCollabService
    .option()
    .then((result) => res.json(result))
    .catch(next);
}

function count(req, res, next) {
  clientCollabService
    .count()
    .then((result) => res.json(result))
    .catch(next);
}

function update(req, res, next) {
  clientCollabService
    .update(req.params.id, req.body)
    .then((result) => res.json(result))
    .catch(next);
}

function enabled(req, res, next) {
  clientCollabService
    .enabled(req.params.id, req.body)
    .then((result) => res.json(result))
    .catch(next);
}

function _delete(req, res, next) {
  clientCollabService
    .delete(req.params.id)
    .then((result) => res.json(result))
    .catch(next);
}
function deleteDetails(req, res, next) {
  clientCollabService
    .deleteDetails(req.params.id)
    .then((result) => res.json(result))
    .catch(next);
}
