import fs from "node:fs";
import { Router } from "express";
import path from "path";
import { fileURLToPath } from "url";

const router = Router();

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const downloadDir = path.resolve(__dirname, "../../downloads/mobile");

const currentApk = {
	fileName: "lst-mobile.apk",
};

router.get("/latest", (_, res) => {
	const apkPath = path.join(downloadDir, currentApk.fileName);

	if (!fs.existsSync(apkPath)) {
		return res.status(404).json({ success: false, message: "APK not found" });
	}

	res.setHeader("Content-Type", "application/vnd.android.package-archive");
	res.setHeader(
		"Content-Disposition",
		`attachment; filename="${currentApk.fileName}"`,
	);

	return res.sendFile(apkPath);
});

router.get("/ehs", (_, res) => {
	const apkPath = path.join(downloadDir, "EHS.apk");

	if (!fs.existsSync(apkPath)) {
		return res.status(404).json({ success: false, message: "APK not found" });
	}

	res.setHeader("Content-Type", "application/vnd.android.package-archive");
	res.setHeader("Content-Disposition", `attachment; filename="EHS.apk"`);

	return res.sendFile(apkPath);
});

router.get("/ehs/xml", (_, res) => {
	const xmlPath = path.join(downloadDir, "enterprisehomescreen.xml");

	if (!fs.existsSync(xmlPath)) {
		return res.status(404).json({
			success: false,
			message: "EHS XML not found",
		});
	}

	res.setHeader("Content-Type", "application/xml");
	res.setHeader(
		"Content-Disposition",
		`attachment; filename="enterprisehomescreen.xml"`,
	);

	return res.sendFile(xmlPath);
});

router.get("/android/upgrade/11", (_, res) => {
	const apkPath = path.join(
		downloadDir,
		"HE_FULL_UPDATE_11-70-20.00-RG-U00-STD-HEL-04.zip",
	);

	if (!fs.existsSync(apkPath)) {
		return res.status(404).json({ success: false, message: "APK not found" });
	}

	//res.setHeader("Content-Type", "application/vnd.android.package-archive");
	res.setHeader("Content-Type", "application/zip");
	res.setHeader(
		"Content-Disposition",
		`attachment; filename="HE_FULL_UPDATE_11.zip"`,
	);

	return res.sendFile(apkPath);
});

router.get("/android/upgrade/13", (_, res) => {
	const apkPath = path.join(
		downloadDir,
		"HE_FULL_UPDATE_13-51-16.00-TG-U00-STD-HEL-04.zip",
	);

	if (!fs.existsSync(apkPath)) {
		return res.status(404).json({ success: false, message: "APK not found" });
	}

	//res.setHeader("Content-Type", "application/vnd.android.package-archive");
	res.setHeader("Content-Type", "application/zip");
	res.setHeader(
		"Content-Disposition",
		`attachment; filename="HE_FULL_UPDATE_13.zip"`,
	);

	return res.sendFile(apkPath);
});

router.get("/android/upgrade/14", (_, res) => {
	const apkPath = path.join(
		downloadDir,
		"HE_FULL_UPDATE_14-38-04.00-UG-U15-STD-HEL-04.zip",
	);

	if (!fs.existsSync(apkPath)) {
		return res.status(404).json({ success: false, message: "APK not found" });
	}

	//res.setHeader("Content-Type", "application/vnd.android.package-archive");
	res.setHeader("Content-Type", "application/zip");
	res.setHeader(
		"Content-Disposition",
		`attachment; filename="HE_FULL_UPDATE_14.zip"`,
	);

	return res.sendFile(apkPath);
});

export default router;
