// 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 {createLog} from "../logger.js";

const app = new OpenAPIHono({strict: false});
const CreateLog = z.object({
    level: z.string().openapi({example: "info"}),
    service: z.string().openapi({example: "server"}),
    message: z.string().openapi({example: "This is a new log posted"}),
});

app.openapi(
    createRoute({
        tags: ["server:logger"],
        summary: "Post a log to the db.",
        method: "post",
        path: "/logs",
        description: "This might be a temp soltuin during the transtion between versions",
        request: {
            body: {content: {"application/json": {schema: CreateLog}}},
        },
        responses: responses(),
    }),
    async (c) => {
        const body = await c.req.json();

        apiHit(c, {endpoint: `api/logger/logs/id`});
        try {
            createLog(body.level, "logger", body.service, body.message);
            return c.json({success: true, message: "A new log was created.", data: []}, 200);
        } catch (error) {
            return c.json({success: false, message: "There was an error clearing the log.", data: error}, 400);
        }
    }
);
export default app;
