import { format } from "date-fns-tz/format";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import { createLog } from "../../logger/logger.js";
import { query } from "../../sqlServer/prodSqlServer.js";
import { forecastData } from "../../sqlServer/querys/psiReport/forecast.js";

// type ArticleData = {
//     id: string
// }
export const getGetPSIForecastData = async (customer: string) => {
	let articles: any = [];
	let queryData = forecastData;
	console.log(customer);
	if (customer) {
		queryData = forecastData.replace("[customer]", customer);
	}

	const { data, error } = (await tryCatch(
		query(queryData, "PSI forecast info"),
	)) as any;

	if (error) {
		createLog(
			"error",
			"datamart",
			"datamart",
			`There was an error getting the forecast info: ${JSON.stringify(error)}`,
		);
		return {
			success: false,
			messsage: `There was an error getting the forecast info`,
			data: error,
		};
	}

	articles = data.data;

	return {
		success: true,
		message: "PSI forecast Data",
		data: articles.map((i: any) => {
			const requirementDate = new Date(i.requirementDate);

			return {
				...i,
				requirementDate: format(requirementDate, "yyyy-MM-dd"),

				dbDate: i.requirementDate,
			};
		}),
	};
};
