// 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 { lotMaterialTransfer } from "../../controller/materials/lotTransfer.js";

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

const LotTransfer = z.object({
    runningNumber: z.number().openapi({ example: 1234 }),
    lotNumber: z.number().openapi({ example: 1235 }),
    originalAmount: z.number().openapi({ example: 457 }),
    level: z.number().openapi({ examples: [0.24, 0.5, 0.75, 0.95] }),
});

app.openapi(
    createRoute({
        tags: ["ocp"],
        summary: "Transfers a gaylord of material to provided lot",
        method: "post",
        path: "/materiallottransfer",
        request: {
            body: { content: { "application/json": { schema: LotTransfer } } },
        },
        responses: responses(),
    }),
    async (c) => {
        //const hours = c.req.query("hours");
        const { data: bodyData, error: bodyError } = await tryCatch(
            c.req.json()
        );
        apiHit(c, { endpoint: "/materiallottransfer", lastBody: bodyData });

        if (bodyError) {
            return c.json({
                success: false,
                message: "You are missing data",
            });
        }

        const { data: transferMaterial, error: transferError } = await tryCatch(
            lotMaterialTransfer(bodyData)
        );

        if (transferError) {
            //console.log(transferError);
            return c.json({
                success: false,
                message:
                    "There was an error transfering the material to the next lot.",
                data: transferError,
            });
        }

        console.log(transferMaterial);

        return c.json({
            success: transferMaterial?.success,
            message: transferMaterial?.message,
            data: transferMaterial?.data,
        });
    }
);
export default app;
