const express = require("express");
const router = express.Router();
const multer = require("multer");
const authorize = require("../../../../middlewares/authorize-sapps");
const leaveApplicationService = require("./leaveApplication.service");

const storage = multer.diskStorage({
	destination: function (req, file, cb) {
		cb(null, "./public/data/documents/leaveapplications/");
	},
	filename: function (req, file, cb) {
		cb(null, Date.now() + "-" + file.originalname);
	},
});

const upload = multer({ storage: storage });

router.post("/create", authorize(), upload.single("filename"), create);
router.post("/create-workflow", authorize(), createWorkflow);
router.post("/", authorize(), getAll);
router.post("/option", authorize(), option);
router.get("/count", authorize(), count);
router.put("/:id", authorize(), upload.single("filename"), update);
router.put("/approve/:id", authorize(), upload.single("filename"), approve);
router.put("/revise/:id", authorize(), upload.single("filename"), revise);
router.put("/reject/:id", authorize(), upload.single("filename"), reject);
router.delete("/:id", authorize(), _delete);

module.exports = router;

function create(req, res, next) {
	const createData = {
		...req.body,
		filename: req.file ? req.file.filename : null,
	};

	leaveApplicationService
		.create(createData)
		.then((result) => res.json(result))
		.catch(next);
}

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

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

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

function update(req, res, next) {
	const updateData = {
		...req.body,
		filename: req.file
			? req.file.filename
			: req.body.filename
				? req.body.filename
				: null,
	};

	leaveApplicationService
		.update(req.params.id, updateData)
		.then((result) => res.json(result))
		.catch(next);
}

function approve(req, res, next) {
	const updateData = {
		...req.body,
		filename: req.file
			? req.file.filename
			: req.body.filename
				? req.body.filename
				: null,
	};

	leaveApplicationService
		.approve(req.params.id, updateData)
		.then((result) => res.json(result))
		.catch(next);
}

function revise(req, res, next) {
	const updateData = {
		...req.body,
		filename: req.file
			? req.file.filename
			: req.body.filename
				? req.body.filename
				: null,
	};

	leaveApplicationService
		.revise(req.params.id, updateData)
		.then((result) => res.json(result))
		.catch(next);
}

function reject(req, res, next) {
	const updateData = {
		...req.body,
		filename: req.file
			? req.file.filename
			: req.body.filename
				? req.body.filename
				: null,
	};

	leaveApplicationService
		.reject(req.params.id, updateData)
		.then((result) => res.json(result))
		.catch(next);
}

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

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