//http://usday1vms006:4000/api/v1/zebra/wrapper1
import { z, createRoute, OpenAPIHono } from "@hono/zod-openapi";
import { responses } from "../../../globalUtils/routeDefs/responses.js";
import { authMiddleware } from "../../auth/middleware/authMiddleware.js";
import { manualTag } from "../controller/tags/manualTag.js";
import { apiHit } from "../../../globalUtils/apiHits.js";

const app = new OpenAPIHono();

const RequestBody = z.object({
    tagNumber: z.string().openapi({ example: "2541" }),
});

app.openapi(
    createRoute({
        tags: ["rfid"],
        summary: "Post a manual Tag",
        method: "post",
        path: "/manualtag",
        middleware: authMiddleware,
        request: {
            body: { content: { "application/json": { schema: RequestBody } } },
        },
        responses: responses(),
    }),
    async (c) => {
        const body =
            (await c.req.json()) ??
            c.json(
                {
                    success: false,
                    message: `Please check that you are sending over a json object`,
                },
                400
            );

        apiHit(c, { endpoint: "/manualtag" });
        try {
            const tag = await manualTag(body.tag, body.area);
            return c.json(
                { success: tag.success, message: tag.message, data: tag.data },
                200
            );
        } catch (error) {
            return c.json(
                {
                    success: true,
                    message: `There was an error with entering  a new tag`,
                    error,
                },
                400
            );
        }
    }
);

export default app;
