const express = require("express");
const router = express.Router();
const authorize = require("../../../../middlewares/authorize-sapps");
const employeeService = require("./employee.service");
const redisClient = require("../../../../redisClient");

function importFromExcel(req, res, next) {
	const modified_by = req.user?.id || req.auth?.sub;
	const body = { ...req.body, modified_by: modified_by || 1, owner: modified_by || 1 };
	employeeService
		.importFromExcel(body)
		.then((result) => res.json(result))
		.catch(next);
}

function previewImportFromExcel(req, res, next) {
	const modified_by = req.user?.id || req.auth?.sub;
	const body = { ...req.body, modified_by: modified_by || 1, owner: modified_by || 1 };
	employeeService
		.previewImportFromExcel(body)
		.then((result) => res.json(result))
		.catch(next);
}

function applyImportFromExcel(req, res, next) {
	const modified_by = req.user?.id || req.auth?.sub;
	const body = { ...req.body, modified_by: modified_by || 1, owner: modified_by || 1 };
	employeeService
		.applyImportFromExcel(body)
		.then((result) => res.json(result))
		.catch(next);
}

router.post("/create", authorize(), create);
router.post("/", authorize(), getAll);
router.post("/option", authorize(), option);
router.post("/import", authorize(), importFromExcel);
router.post("/import/preview", authorize(), previewImportFromExcel);
router.post("/import/apply", authorize(), applyImportFromExcel);
router.get("/count", authorize(), count);
router.put("/:id", authorize(), update);
router.put("/enabled/:id", authorize(), enabled);
router.post("/bulk-update", authorize(), bulkUpdate);
router.delete("/:id", authorize(), _delete);

module.exports = router;

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

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

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

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

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

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

function bulkUpdate(req, res, next) {
	// Get user role for permission check
	const userId = req.user?.id || req.auth?.sub;
	if (!userId) {
		return res.status(401).json({
			success: false,
			message: "User not authenticated!",
		});
	}

	// Pass user_id to service for permission validation
	employeeService
		.bulkUpdate({ ...req.body, user_id: userId })
		.then((result) => res.json(result))
		.catch(next);
}

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