import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import { apiHit } from "../../../globalUtils/apiHits.js";
import { getShipmentPallets } from "../controller/getShipmentPallets.js";

const app = new OpenAPIHono();

const ShipmentID = z.object({
    shipmentID: z.string().optional().openapi({ example: "14558" }),
});

app.openapi(
    createRoute({
        tags: ["ocme"],
        summary: "Post New running number to be picked up.",
        method: "post",
        path: "/GetShipmentPallets",
        request: {
            body: {
                content: {
                    "application/json": { schema: ShipmentID },
                },
            },
        },
        responses: {
            200: {
                content: {
                    "application/json": {
                        schema: z.object({
                            success: z.boolean().openapi({ example: true }),
                            message: z.string().openapi({ example: "Starter" }),
                            // data: z
                            // .array(z.object({sscc: z.string().optional()}))
                            // .optional()
                            // .openapi({example: []}),
                        }),
                    },
                },
                description: "Response message",
            },
            400: {
                content: {
                    "application/json": {
                        schema: z.object({
                            success: z.boolean().openapi({ example: false }),
                            message: z
                                .string()
                                .optional()
                                .openapi({ example: "Internal Server error" }),
                            data: z
                                .array(z.object({}))
                                .optional()
                                .openapi({ example: [] }),
                        }),
                    },
                },
                description: "Internal Server Error",
            },
            // 401: {
            //     content: {
            //         "application/json": {
            //             schema: z.object({message: z.string().optional().openapi({example: "Unauthenticated"})}),
            //         },
            //     },
            //     description: "Unauthorized",
            // },
            // 500: {
            //     content: {
            //         "application/json": {
            //             schema: z.object({message: z.string().optional().openapi({example: "Internal Server error"})}),
            //         },
            //     },
            //     description: "Internal Server Error",
            // },
        },
    }),
    async (c) => {
        // make sure we have a vaid user being accessed thats really logged in
        try {
            const data = await c.req.json();
            apiHit(c, { endpoint: "/getshipmentpallets", lastBody: data });

            console.log;

            if (!data.shipmentID) {
                return c.json(
                    {
                        success: false,
                        message:
                            "You are missing the shipment id please try again.",
                        data: [],
                    },
                    400
                );
            }

            const shiptmentData = await getShipmentPallets(data.shipmentID);

            return c.json(
                {
                    success: shiptmentData.success,
                    message: shiptmentData.message,
                    data: shiptmentData.data ?? [],
                },
                200
            );
        } catch (error) {
            return c.json(
                {
                    success: false,
                    message: "There was an error getting the shipment data.",
                    data: error,
                },
                400
            );
        }
    }
);
export default app;
