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

const router = Router();

router.post("/", async (req, res) => {
	const body = req.body;

	const lane = body.lane.split("#");

	// check if the plant has warehousing activated
	const featureQ = sqlQuerySelector(`featureCheck`) as SqlQuery;

	const { data: fd, error: fe } = await tryCatch(
		prodQuery(featureQ.query, `Running feature check`),
	);

	if (fe) {
		return returnFunc({
			success: false,
			level: "error",
			module: "datamart",
			subModule: "query",
			message: `feature check failed`,
			data: fe as any,
			notify: false,
		});
	}

	console.log(fd);

	const laneData = await runProdApi({
		method: "post",
		endpoint: "/public/v1.1/Warehousing/GetWarehouseUnits",
		data: [
			{
				laneIds: [lane[2]],
			},
		],
	});

	return apiReturn(res, {
		success: true,
		level: "info",
		module: "mobile",
		subModule: "lane check",
		message: `all data for lane Id: ${lane}`,
		data: laneData?.data ?? [],
		status: 200,
	});
});

export default router;
