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

router.post("/create", authorize(), create);
router.post("/create-distributor-outlet", authorize(), createDistOutlet);
router.post("/create-distributor-item", authorize(), createDistItem);
router.post("/create-distributor-branch", authorize(), createDistBranch);
router.get("/", authorize(), getAll);
router.get("/distributor-outlet", authorize(), getDistOutlet);
router.get("/distributor-item", authorize(), getDistItem);
router.get("/distributor-branch", authorize(), getDistBranch);
router.get("/filter", authorize(), filter);
router.get("/distributor-outlet/filter", authorize(), filterDistOutlet);
router.get("/distributor-item/filter", authorize(), filterDistItem);
router.get("/distributor-branch/filter", authorize(), filterDistBranch);
router.get("/option", authorize(), option);
router.get("/count", authorize(), count);
router.put("/:id", authorize(), update);
router.put("/distributor-outlet/:id", authorize(), updateDistOutlet);
router.put("/distributor-item/:id", authorize(), updateDistItem);
router.put("/distributor-branch/:id", authorize(), updateDistBranch);
router.put("/enabled/:id", authorize(), enabled);
router.put("/distributor-outlet/enabled/:id", authorize(), distOutletenabled);
router.put("/distributor-item/enabled/:id", authorize(), distItemenabled);
router.put("/distributor-branch/enabled/:id", authorize(), distBranchenabled);
router.delete("/:id", authorize(), _delete);
router.delete("/distributor-outlet/:id", authorize(), deleteDistOutlet);
router.delete("/distributor-item/:id", authorize(), deleteDistItem);
router.delete("/distributor-branch/:id", authorize(), deleteDistBranch);

module.exports = router;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

function deleteDistOutlet(req, res, next) {
  distributorService
    .deleteDistOutlet(req.params.id)
    .then((result) => res.json(result))
    .catch(next);
}

function deleteDistItem(req, res, next) {
  distributorService
    .deleteDistItem(req.params.id)
    .then((result) => res.json(result))
    .catch(next);
}

function deleteDistBranch(req, res, next) {
  distributorService
    .deleteDistBranch(req.params.id)
    .then((result) => res.json(result))
    .catch(next);
}
