var express = require("express");
const { verifyRegister } = require("../middleware");
const authJwt = require("../middleware/authJwt");
const controller = require("../controllers/authorizController");

const userController = require("../controllers/userController");
const eventController = require("../controllers/eventController");
const router = express.Router();

router.use((req, res, next) => {
	res.header(
		"Access-Control-Allow-Headers",
		"x-access-token, Origin, Content-Type, Accept",
	);
	next();
});

router.post("/register", [
	verifyRegister.checkDuplicateUsernameOrEmail,
	verifyRegister.checkRolesExisted,
	userController.registerUserController,
]);

router.post("/login", userController.loginUserController);

router.post("/auth", authJwt.verifyToken);

// posílání kontentu dle role
router.post("/all", controller.allAccess);
router.post("/user", controller.userBoard);
router.post("/admin", controller.adminBoard);

// CRUD operace v rámci eventů kalendáře
router.post("/api/events", eventController.saveNewEvent);
router.post("/api/events/array", eventController.saveNewEventArray);
router.get("/api/events", eventController.getAllEvents);
router.get("/api/events/all", eventController.getAllEventsPure);
router.delete("/api/events/:id", eventController.deleteEvent);
router.put("/api/events/:id", eventController.changeEvent);

// Operace v rámci skupin a jejich uživatelů
router.get("/api/users", userController.fetchAllUsers);
router.get("/api/users/group/:groupId", userController.fetchUser);
router.put("/api/users/:id", userController.updateGroup);

// Preference uživatelů
router.get("/api/user/preferences/:userId", userController.getPreference);
router.put("/api/user/preferences/:userId", userController.updatePreference);

module.exports = router;
