const fetch = require("node-fetch");
const fs = require("fs");
const DBD = require("../../../../index");

module.exports = {
	page: "/debug",
	execute: async (req, res, app, config, themeConfig, info) => {
		/*
			Do not remove this page.
			It will be used with support in the discord server.
		*/
		if (!req.session.user) return res.redirect("/discord?r=/debug/");
		if (!config.ownerIDs?.includes(req.session.user.id)) return res.redirect("/");

		const onlineFiles = {
			index: await fetch("https://cdn.jsdelivr.net/gh/Assistants-Center/DBD-Soft-UI/views/index.ejs"),
			guild: await fetch("https://cdn.jsdelivr.net/gh/Assistants-Center/DBD-Soft-UI/views/guild.ejs"),
			guilds: await fetch("https://cdn.jsdelivr.net/gh/Assistants-Center/DBD-Soft-UI/views/guilds.ejs"),
		};

		onlineFiles.index = await onlineFiles.index.text();
		onlineFiles.guild = await onlineFiles.guild.text();
		onlineFiles.guilds = await onlineFiles.guilds.text();

		const localFiles = {
			index: await fs.readFileSync(`${__dirname}/../../views/index.ejs`, "utf-8"),
			guild: await fs.readFileSync(`${__dirname}/../../views/settings.ejs`, "utf-8"),
			guilds: await fs.readFileSync(`${__dirname}/../../views/guilds.ejs`, "utf-8"),
		};

		res.render("debug", {
			onlineFiles,
			localFiles,
			rawUptime: process.uptime(),
			nodeVersion: process.version,
			themeConfig,
			discordVersion: require("discord.js").version,
			dbdVersion: DBD.version,
			themeVersion: require("dbd-soft-ui").version,
			req,
			config,
			info,
		});
	},
};