import { desc, gte, sql } from "drizzle-orm";
import { Router } from "express";
import { db } from "../db/db.controller.js";
import { opendockApt } from "../db/schema/opendock.schema.js";
import { apiReturn } from "../utils/returnHelper.utils.js";
import { tryCatch } from "../utils/trycatch.utils.js";

const r = Router();

r.get("/", async (_, res) => {
	//const limit

	const daysCreated = 30;

	const { data } = await tryCatch(
		db
			.select()
			.from(opendockApt)
			.where(
				gte(
					opendockApt.createdAt,
					sql.raw(`NOW() - INTERVAL '${daysCreated} days'`),
				),
			)
			.orderBy(desc(opendockApt.createdAt))
			.limit(500),
	);

	apiReturn(res, {
		success: true,
		level: "info",
		module: "opendock",
		subModule: "apt",
		message: `The first ${data?.length} Apt(s) that were created in the last ${daysCreated} `,
		data: data ?? [],
		status: 200,
	});
});

export default r;
