// 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";
import {getLogs} from "../controller/getLogs.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: "Gets logs.",
        method: "get",
        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 query = await c.req.queries();

        apiHit(c, {endpoint: `api/logger/logs`});
        try {
            const logData = await getLogs(query);
            return c.json({success: logData?.success, message: logData?.message, data: logData?.data}, 200);
        } catch (error) {
            return c.json({success: false, message: "There was an error clearing the log.", data: error}, 400);
        }
    }
);
export default app;
