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

// routes
import manualLabelLog from "./routes/labeling/manualPrintLog.js";
import getPrinters from "./routes/printers/getPritners.js";
import { db } from "../../../database/dbclient.js";
import { settings } from "../../../database/schema/settings.js";
import updateprinters from "./routes/printers/updatePrinters.js";
import { updatePrinters } from "./controller/printers/updatePrinters.js";
import getLots from "./routes/lots/getLots.js";
import getLabels from "./routes/labeling/getLabels.js";
import { dycoConnect } from "./controller/specialProcesses/dyco/plcConnection.js";
import dycoCon from "./routes/specialProcesses/dyco/connection.js";
import dycoClose from "./routes/specialProcesses/dyco/closeConnection.js";
import manualprint from "./routes/labeling/manualPrint.js";
import { assignedPrinters } from "./utils/checkAssignments.js";
import { printerCycle } from "./controller/printers/printerCycle.js";
import stopPrinterCycle from "./routes/printers/stopCycle.js";
import startPrinterCycle from "./routes/printers/startCycle.js";
import AutostartPrinterCycle from "./routes/printers/autoLabelerStart.js";
import AutostopPrinterCycle from "./routes/printers/autoLabelerStop.js";
import { deleteLabels } from "../../globalUtils/dbCleanUp/labelCleanUp.js";
import bookInLabel from "./routes/labeling/bookIn.js";
import labelRatio from "./routes/labeling/getLabelRatio.js";
import resetRatio from "./routes/labeling/resetLabelRatio.js";
import materialTransferLot from "./routes/materials/lotTransfer.js";
import pendingTransfers from "./routes/materials/currentPending.js";
import { createPlcMonitor } from "./controller/specialProcesses/zechettis/plcController.js";
import { zechettiConnect } from "./controller/specialProcesses/zechettis/zechettiConnect.js";

const app = new OpenAPIHono();

const routes = [
    manualLabelLog,
    //printer
    getPrinters,
    updateprinters,
    startPrinterCycle,
    stopPrinterCycle,
    AutostartPrinterCycle,
    AutostopPrinterCycle,
    // lots
    getLots,
    // labeling
    getLabels,
    manualprint,
    bookInLabel,
    labelRatio,
    resetRatio,
    //dyco
    dycoCon,
    dycoClose,
    // materials
    materialTransferLot,
    pendingTransfers,
] as const;
const setting = await db.select().from(settings);

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

app.all("/ocp/*", (c) => {
    return c.json({
        success: false,
        message: "You have encounters a ocp route that dose not exist.",
    });
});

/**
 * Get the settings so we only run items when they are truly active
 */
const dycoActive = setting.filter((n) => n.name == "dycoConnect");
const ocpActive = setting.filter((n) => n.name === "ocpActive");
const zechetti = setting.filter((n) => n.name == "zechetti");
// run the printer update on restart just to keep everything good

// do the intnal connection to the dyco
setTimeout(() => {
    if (dycoActive[0]?.value === "1") {
        dycoConnect();
    }
}, 3 * 1000);

// if zechetti plc is wanted we will connect
setTimeout(() => {
    if (zechetti[0]?.value === "1") {
        zechettiConnect();
    }
}, 3 * 1000);

// check for printers being assigned
setInterval(() => {
    if (ocpActive[0]?.value === "1") {
        assignedPrinters();
    }
}, 60 * 1000);

// start the printer process after everything else is up ad running
setTimeout(async () => {
    if (ocpActive[0]?.value === "1") {
        await updatePrinters();
        await assignedPrinters();
        printerCycle();
        // printerCycleAutoLabelers();
    }
}, 10 * 1000);

// labelcleanup
deleteLabels();
setInterval(() => {
    deleteLabels();
}, 60 * 60 * 24 * 1000);

setInterval(() => {
    updatePrinters();
}, 1000 * 60 * 60 * 24);

export default app;
