import { OpenAPIHono } from "@hono/zod-openapi";

const app = new OpenAPIHono();

import stats from "./route/stats.js";
import history from "./route/invHistory.js";
import { createJob } from "../notifications/utils/processNotifications.js";
import { historicalInvIMmport } from "./utils/historicalInv.js";
import { tryCatch } from "../../globalUtils/tryCatch.js";
import { query } from "../sqlServer/prodSqlServer.js";
import { shiftChange } from "../sqlServer/querys/misc/shiftChange.js";
import { createLog } from "../logger/logger.js";
import lastPurch from "./route/getLastPurchPrice.js";
import lastSales from "./route/getLastSalesPrice.js";
import gpData from "./route/getGpData.js";
import consumptionData from "./route/getProductionConsumption.js";
import regrind from "./route/getregrind.js";
import soldItems from "./route/getSoldItems.js";
import purchased from "./route/getPurchased.js";

const routes = [
    stats,
    history,
    lastPurch,
    lastSales,
    gpData,
    consumptionData,
    regrind,
    soldItems,
    purchased,
] as const;

const appRoutes = routes.forEach((route) => {
    app.route("/eom", route);
});

setTimeout(async () => {
    const { data: shift, error: shiftError } = (await tryCatch(
        query(shiftChange, "shift change from material.")
    )) as any;

    if (shiftError) {
        createLog(
            "error",
            "eom",
            "eom",
            "There was an error getting the shift times will use fallback times"
        );
    }

    // shift split
    const shiftTimeSplit = shift?.data[0]?.shiftChange.split(":");

    const cronSetup = `${
        shiftTimeSplit?.length > 0 ? `${parseInt(shiftTimeSplit[1])}` : "0"
    } ${
        shiftTimeSplit?.length > 0 ? `${parseInt(shiftTimeSplit[0])}` : "7"
    } * * *`;

    //console.log(cronSetup);
    createJob("eom_historical_inv", cronSetup, historicalInvIMmport);
}, 5 * 1000);
// the time we want to run the hostircal data should be  the same time the historical data run on the server
// getting this from the shift time

export default app;
