import { eq } from "drizzle-orm";
import { db } from "../../../../../../database/dbclient.js";
import { settings } from "../../../../../../database/schema/settings.js";
import { tryCatch } from "../../../../../globalUtils/tryCatch.js";
import { createLog } from "../../../../logger/logger.js";

const st = 5;
const sm = 55;
const et = 6;
const em = 5;

export const billingCheck = async () => {
    const now = new Date();
    const startTime = new Date();
    const endTime = new Date();

    let isOutsideBilling = false;
    const { data, error } = await tryCatch(db.select().from(settings));
    if (error) {
        return { success: false, message: "Error in getting settings." };
    }
    const plant = data.filter((n) => n.name === "plantToken");
    // set everything up
    startTime.setHours(st, sm, 0, 0);
    endTime.setHours(et, em, 0, 0);

    if (plant[0].value === "usflo1") {
        if (now >= startTime && now <= endTime) {
            createLog(
                "warn",
                "specialProcess",
                "ocp",
                `You are inside the billing window no labels will print! please wait`
            );
            isOutsideBilling = true;
        } else {
            createLog(
                "info",
                "specialProcess",
                "ocp",
                "Out side billing window ok to print"
            );
        }
    } else {
        createLog(
            "debug",
            "specialProcess",
            "ocp",
            "This plant dose not check for billing"
        );
    }

    return isOutsideBilling;
};
