// an external way to creating logs
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import { responses } from "../../../../globalUtils/routeDefs/responses.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
import { apiHit } from "../../../../globalUtils/apiHits.js";

import { pendingJobs } from "../../controller/materials/lotTransfer.js";
import { format, formatDuration, intervalToDuration } from "date-fns";

const app = new OpenAPIHono({ strict: false });

app.openapi(
    createRoute({
        tags: ["ocp"],
        summary: "Returns pending transfers",
        method: "get",
        path: "/pendingtransfers",

        responses: responses(),
    }),
    async (c) => {
        apiHit(c, { endpoint: "/pendingtransfers" });

        const pending = Array.from(pendingJobs.entries()).map(
            ([runningNumber, job]) => {
                const duration = intervalToDuration({
                    start: new Date(),
                    end: job.scheduledFor,
                });
                return {
                    runningNumber,
                    lot: job.data?.lotNumber,
                    newQty: job.newQty,
                    consumeLot: job.consumeLot,
                    scheduledFor: format(job.scheduledFor, "M/d/yyyy HH:mm"),
                    remainingMs: formatDuration(duration, {
                        format: ["hours", "minutes", "seconds"],
                    }),
                };
            }
        );

        //console.log(pending);
        return c.json({
            success: true,
            message: "Current Pending trnasfers",
            data: [pending],
        });
    }
);
export default app;
