// an external way to creating logs
//@ts-nocheck
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";
import {streamLogs} from "../controller/streamLogs.js";
import {streamSSE} from "hono/streaming";

const app = new OpenAPIHono({strict: false});

app.openapi(
    createRoute({
        tags: ["server:logger"],
        summary: "Streams the logs to the frontend.",
        method: "get",
        path: "/logs/stream",
        description: "This should only be used on the event you need to monitor logs.",

        responses: {
            200: {
                content: {
                    "application/json": {schema: z.object({message: z.string().optional()})},
                },
                description: "Response message",
            },
        },
    }),
    async (c) => {
        apiHit(c, {endpoint: `api/logger/logs`});
        c.header("Content-Type", "text/event-stream");
        c.header("Cache-Control", "no-cache");
        c.header("Connection", "keep-alive");
        return streamSSE(c, async (stream) => {
            let id = 0;
            const encoder = new TextEncoder();
            while (true) {
                const message = `It is ${new Date().toISOString()}`;
                await stream.writeSSE({
                    data: message,
                    event: "time-update",
                    id: String(id++),
                });
                encoder.encode(`data: ${JSON.stringify({type: "progress", data: id})}\n\n`);
                await stream.sleep(1000);
            }
        });
    }
);
export default app;
