// an external way to creating logs
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import { responses } from "../../../../../globalUtils/routeDefs/responses.js";
import { authMiddleware } from "../../../../auth/middleware/authMiddleware.js";
import { tryCatch } from "../../../../../globalUtils/tryCatch.js";
import { dycoConnect } from "../../../controller/specialProcesses/dyco/plcConnection.js";
import { apiHit } from "../../../../../globalUtils/apiHits.js";

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

app.openapi(
    createRoute({
        tags: ["ocp:dyco"],
        summary: "Connects to the dyco.",
        method: "get",
        path: "/dycoconnect",
        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 body = await c.req.json();
        apiHit(c, { endpoint: `dycoconnect` });
        // const authHeader = c.req.header("Authorization");

        // const token = authHeader?.split("Bearer ")[1] || "";
        // let user: User;

        // try {
        //     const payload = await verify(token, process.env.JWT_SECRET!);
        //     user = payload.user as User;
        // } catch (error) {
        //     console.log(error);
        //     return c.json({message: "Unauthorized"}, 401);
        // }

        const { data, error } = await tryCatch(dycoConnect());

        const dataError: any = error;
        if (error) {
            return c.json({
                success: false,
                message: "Error in connecting to dyco",
                data: dataError?.data,
            });
        }
        const getData: any = data;
        return c.json({
            success: getData?.success,
            message: getData?.message,
            data: getData?.data ?? [],
        });
    }
);
export default app;
