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

const app = new OpenAPIHono({ strict: false });
const Body = z.object({
    includeRunnningNumbers: z.string().openapi({ example: "x" }),
});
app.openapi(
    createRoute({
        tags: ["dataMart"],
        summary: "Returns the psiproductiondata.",
        method: "get",
        path: "/psiproductiondata",
        request: {
            body: {
                content: {
                    "application/json": { schema: Body },
                },
            },
        },
        responses: responses(),
    }),
    async (c) => {
        const q: any = c.req.queries();

        // make sure we have a vaid user being accessed thats really logged in
        apiHit(c, { endpoint: "/psiproductiondata" });
        //console.log(articles["avs"][0]);
        const { data, error } = await tryCatch(
            psiGetProductionData(
                q["avs"] ? q["avs"][0] : null,
                q["startDate"] ? q["startDate"][0] : null,
                q["endDate"] ? q["endDate"][0] : null
            )
        );

        if (error) {
            console.log(error);
            return c.json(
                {
                    success: false,
                    message: "There was an error getting the production.",
                    data: error,
                },
                400
            );
        }

        //console.log(data);

        return c.json(
            {
                success: data.success,
                message: data.message,
                data: data.data,
            },
            data.success ? 200 : 400
        );
    }
);
export default app;
