import { apiHandler, db } from "@/helpers/api";

export default apiHandler({
	get: isAdmin,
});

async function isAdmin(req, res) {
	const userID = req.userID;

	if (!db.initialized) await db.initialize();

	try {
		const userData = await db.User.findByPk(userID);

		if (!userData) return res.status(404).json({ error: "Unknown!" });

		if (userData.admin < 1) {
			return res.status(401).json({ error: "Unauthorized access." });
		}

		return res.status(200).json({});
	} catch (error) {
		console.error("Eroare la preluarea statisticilor:", error);
		return res.status(500).json({ error: "Eroare la preluarea statisticilor" });
	}
}
