import { Router } from "express";
import { prodQuery } from "../prodSql/prodSqlQuery.controller.js";
import {
	type SqlQuery,
	sqlQuerySelector,
} from "../prodSql/prodSqlQuerySelector.utils.js";
import { apiReturn } from "../utils/returnHelper.utils.js";
import { tryCatch } from "../utils/trycatch.utils.js";

const r = Router();

r.get("/", async (_, res) => {
	const activeDocks = sqlQuerySelector(`outbound.docks`) as SqlQuery;

	if (!activeDocks.success) {
		return apiReturn(res, {
			success: false,
			level: "error",
			module: "dockdoor",
			subModule: "docks",
			message: `There was an error getting the docks query.`,
			data: [],
			status: 400,
		});
	}

	const { data, error } = await tryCatch(
		prodQuery(activeDocks.query, "Current Active Docks"),
	);

	if (error) {
		return apiReturn(res, {
			success: false,
			level: "error",
			module: "dockdoor",
			subModule: "newDock",
			message: `There was an error getting the docks.`,
			data: (error as any) ?? ([] as any),
			status: 400,
		});
	}

	return apiReturn(res, {
		success: true,
		level: "info",
		module: "dockdoor",
		subModule: "docks",
		message: `Current active docks.`,
		data: (data.data as any) ?? ([] as any),
		status: 200,
	});
});

export default r;
