const db = require("quick.db");

module.exports = {
	page: "/admin",
	execute: async (req, res, app, config, themeConfig, info, database) => {
		if (!req.session.user) return res.redirect("/discord?r=/admin/");
		if (!config.ownerIDs?.includes(req.session.user.id)) return res.redirect("/");
		if (!themeConfig.nodeactyl && themeConfig.admin?.pterodactyl?.enabled) return res.send("Unable to contact Pterodactyl, are your details correct?");

		async function getServers() {
			if (!themeConfig?.admin?.pterodactyl?.enabled) return [];

			const serverData = [];
			const serverUUIDS = themeConfig?.admin?.pterodactyl?.serverUUIDs;
			for (const uuid of serverUUIDS) {
				const dataStatus = await themeConfig?.nodeactyl?.getServerStatus(uuid);
				const data = await themeConfig?.nodeactyl?.getServerDetails(uuid);

				serverData.push({
					name: data.name.toString(),
					uuid: data.uuid.toString(),
					desc: data.description.toString(),
					node: data.node.toString(),
					status: dataStatus.toString(),
				});
			}

			return serverData;
		}

		const d = await getServers();

		res.render("admin", {
			req,
			sData: d,
			ldata: await database.get("logs"),
			themeConfig: req.themeConfig,
			node: themeConfig.nodeactyl,
			bot: config.bot,
			allFeedsUsed: (db.get("feeds.one") && db.get("feeds.two") && db.get("feeds.three")) ? true : false,
			config,
			require,
		});
	},
};