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

import { lastPurchase } from "../controller/getLastPurchasesPrice.js";
import { lastSales } from "../controller/getLastestSalesPrice.js";

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

app.openapi(
    createRoute({
        tags: ["eom"],
        summary: "Returns last sales price.",
        method: "get",
        path: "/lastsalesprice",
        responses: responses(),
    }),
    async (c) => {
        //const body = await c.req.json();
        // make sure we have a vaid user being accessed thats really logged in
        const q: any = c.req.queries();
        apiHit(c, { endpoint: "/lastsalesprice" });
        try {
            const res = await lastSales(
                q["month"] ? q["month"][0] : null,
                q["includePlantToken"] ? true : false
            );

            return c.json(
                { success: res.success, message: res.message, data: res.data },
                200
            );
        } catch (error) {
            return c.json(
                {
                    success: false,
                    message: "There was an error posting the eom stat.",
                    data: error,
                },
                400
            );
        }
    }
);
export default app;
