//http://usday1vms006:4000/api/v1/zebra/wrapper1
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";

// Define the response schema
const responseSchema = z.object({
    success: z.boolean().openapi({example: true}),
    message: z.string().optional(),
});

const app = new OpenAPIHono();

const ParamsSchema = z.object({
    printer: z
    .string()
    .min(3)
    .openapi({
        param: {
            name: "printer",
            in: "path",
        },
        example: "Line1",
    }),
});

app.openapi(
    createRoute({
        tags: ["printer"],
        summary: "Printer Alert",
        method: "post",
        path: "/{printer}",
        request: {
            params: ParamsSchema,
        },
        responses: {
            200: {
                content: {
                    "application/json": {schema: responseSchema},
                },
                description: "Response message",
            },
            400: {
                content: {
                    "application/json": {
                        schema: z.object({message: z.string().optional().openapi({example: "Internal Server error"})}),
                    },
                },
                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) => {
        const {printer} = c.req.valid("param");

        const contentType = c.req.header("Content-Type") || "";
        const boundaryMatch = contentType.match(/boundary=(.*)$/);

        if (!boundaryMatch) {
            return c.json({message: "No boundary found in Content-Type"}, 400);
        }

        const boundary = boundaryMatch[1];
        const body = await c.req.text(); // Get the body of the request

        // Split the body by the boundary (adding extra dashes before the boundary)
        const parts = body.split(`--${boundary}`);
        console.log(parts);
        // Remove the first and last empty parts (they are just before and after the boundaries)
        const formDataParts = parts.slice(1, parts.length - 1);

        // console.log(formDataParts);

        // formDataParts.forEach((part, index) => {
        //     // Split the part into headers and body
        //     const [headers, data] = part.split("\r\n\r\n");

        //     // Log the part index and data for debugging
        //     console.log(`Part ${index + 1}:`);
        //     console.log("Headers:", headers);
        //     console.log("Data:", decodeURIComponent(data));
        // });

        return c.json({success: true, message: `New info from ${printer}`}, 200);
    }
);

export default app;
