// an external way to creating logs
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import { apiHit } from "../../../../globalUtils/apiHits.js";
import { responses } from "../../../../globalUtils/routeDefs/responses.js";
import type { User } from "../../../../types/users.js";
import { verify } from "hono/jwt";
import { manualPrint } from "../../controller/labeling/manualLabelLog.js";
import { authMiddleware } from "../../../auth/middleware/authMiddleware.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.js";

const app = new OpenAPIHono({ strict: false });
const CreateLog = z.object({
    line: z.string().openapi({ example: "info" }),
    initials: z.string().openapi({ example: "server" }),
    printReason: z.string().openapi({ example: "This is a new log posted" }),
    additionalComments: z
        .string()
        .optional()
        .openapi({ example: "Some reason why we did this." }),
});

app.openapi(
    createRoute({
        tags: ["ocp"],
        summary: "Post the log for manaulprinting.",
        method: "post",
        path: "/manuallabellog",
        //middleware: authMiddleware,
        //description: "This might be a temp soltuin during the transtion between versions",
        request: {
            body: { content: { "application/json": { schema: CreateLog } } },
        },
        responses: responses(),
    }),
    async (c) => {
        const { data: body, error } = await tryCatch(c.req.json());
        apiHit(c, { endpoint: "/manuallabellog", lastBody: body });
        if (error) {
            return c.json(
                {
                    success: false,
                    message: "Missing Data.",
                },
                400
            );
        }
        apiHit(c, { endpoint: `api/logger/logs/id` });
        try {
            //const data = {...body, add_user: user.username};
            const printLog: any = await manualPrint(body);
            return c.json(
                {
                    success: printLog.success,
                    message: printLog.message,
                    data: printLog.data ?? [],
                },
                200
            );
        } catch (error) {
            return c.json(
                {
                    success: false,
                    message: "There was an error clearing the log.",
                    data: error,
                },
                400
            );
        }
    }
);
export default app;
