import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import { apiHit } from "../../../globalUtils/apiHits.js";
import { responses } from "../../../globalUtils/routeDefs/responses.js";
import { getfinanceAudit } from "../controller/getFinanceAudit.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";

const app = new OpenAPIHono({ strict: false });
const EomStat = z.object({
    plant: z.string().openapi({ example: "Salt Lake City" }),
    userRan: z.string().openapi({ example: "smith034" }),
    eomSheetVersion: z.string().openapi({ example: "0.0.223" }),
});

app.openapi(
    createRoute({
        tags: ["dataMart"],
        summary:
            "Returns inventory to date and current sales prices and booking date of the inventory.",
        method: "get",
        path: "/getfinanceaudit",

        responses: responses(),
    }),
    async (c: any) => {
        //const body = await c.req.json();
        // make sure we have a vaid user being accessed thats really logged in
        const { data, error } = (await tryCatch(c.req.query())) as any;

        //console.log(data.date);

        if (error) {
            return {
                success: false,
                message: "Missing Mandatory Data",
                data: error,
            };
        }

        if (!data.date) {
            return {
                success: false,
                message: "Missing Date",
                data: [],
            };
        }
        apiHit(c, { endpoint: "/getfinanceaudit", lastBody: data });

        try {
            return c.json(
                {
                    success: true,
                    message: `Inventory older than ${data.date}`,
                    data: await getfinanceAudit(data.date),
                },
                200
            );
        } catch (error) {
            return c.json(
                {
                    success: false,
                    message: "There was an error getting inventory data",
                    data: error,
                },
                400
            );
        }
    }
);
export default app;
