import type { Request, Response } from "express";
import { Router } from "express";
import z, { success } from "zod";
import { db } from "../../../../pkg/db/db.js";
import { forecastData } from "../../../../pkg/db/schema/forecastEDIData.js";
import { tryCatch } from "../../../../pkg/utils/tryCatch.js";
import { forecastEdiData } from "../../controller/demandManagement/forecastEDIData.js";

export const Preprint = z.object({
	scannerId: z.number(),
	lotNr: z.number(),
	machineId: z.number(), // 457=22, 458=23
	printerId: z.number(), // 457=22, 458=23
	layoutId: z.number(),
	numberOfCopies: z.number(),
	qtyToPrint: z.number().default(1),
});

const router = Router();

router.post("/forecastData", async (req: Request, res: Response) => {
	await forecastEdiData(req.body);

	res.status(200).json({ success: true, message: "Forecast Data", data: [] });
});

// quick fix for getting the data
router.get("/forecastData", async (req: Request, res: Response) => {
	const { data, error } = await tryCatch(db.select().from(forecastData));

	if (error) {
		return res.status(400).json({
			success: false,
			message: "Error getting forecast data",
			error: error,
		});
	}

	res.status(200).json({ success: true, message: "Forecast Data", data: data });
});

export default router;
