import type { Express } from "express";

import { apiReturn } from "../utils/returnHelper.utils.js";
import { datamartData } from "./datamartData.utlis.js";
import runQuery from "./getDatamart.route.js";

export const setupDatamartRoutes = (baseUrl: string, app: Express) => {
	// the sync callback.
	// app.get(`${baseUrl}/api/datamart/sync`, async (req, res) => {
	// 	const { time } = req.query;
	// 	const now = new Date();

	// 	const minutes = parseInt(time as string, 10) || 15;
	// 	const cutoff = new Date(now.getTime() - minutes * 60 * 1000);

	// 	const results = await db
	// 		.select()
	// 		.from(datamart)
	// 		.where(time ? gte(datamart.upd_date, cutoff) : sql`true`);

	// 	return apiReturn(res, {
	// 		success: true,
	// 		level: "info",
	// 		module: "datamart",
	// 		subModule: "query",
	// 		message: `All Queries older than ${parseInt(process.env.QUERY_CHECK?.trim() || "15", 10)}min `,
	// 		data: results,
	// 		status: 200,
	// 	});
	// });

	//setup all the routes

	app.use(`${baseUrl}/api/datamart`, runQuery);

	// just sending a get on datamart will return all the queries that we can call.
	app.get(`${baseUrl}/api/datamart`, async (_, res) => {
		// const queries = await db
		// 	.select({
		// 		id: datamart.id,
		// 		name: datamart.name,
		// 		description: datamart.description,
		// 		options: datamart.options,
		// 		version: datamart.version,
		// 		upd_date: datamart.upd_date,
		// 	})
		// 	.from(datamart)
		// 	.where(and(eq(datamart.active, true), eq(datamart.public, true)));

		return apiReturn(res, {
			success: true,
			level: "info",
			module: "datamart",
			subModule: "query",
			message: "All active queries we can run",
			data: datamartData,
			status: 200,
		});
	});
};
