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";

// strapper related issues
export let strapperActive = false;
export let stapperFaulted = false;
export let strapperFaultCount = 3; // move to db so we can control it outside the app

export const strapperFaults = async (tagData: any) => {
    const { data, error } = await tryCatch(db.select().from(settings));

    // failed to get settings
    if (error) {
        return { success: false, message: "Failed to get settings." };
    }
    const strapperCheckSetting = data.filter((n) => n.name === "strapperCheck");

    // strapper error is off
    if (strapperCheckSetting[0]?.value === "0") {
        return;
    }

    // strapper error is on
    if (strapperCheckSetting[0]?.value === "1") {
        // faulted and still has a check or 2 to go
        if (stapperFaulted && strapperFaultCount > 0) {
            createLog(
                "warn",
                "dyco",
                "ocp",
                `There was a strapper error, remaining pallets to check ${strapperFaultCount}.`
            );
            strapperFaultCount = strapperFaultCount - 1;
            return {
                success: true,
                message: `There was a strapper error, remaining pallets to check ${strapperFaultCount}.`,
            };
        } else {
            // no more checks needed clearing everything
            createLog(
                "debug",
                "dyco",
                "ocp",
                `Strapper check is active but not faulted, remaining pallets to check ${strapperFaultCount}.`
            );

            // reset everything
            stapperFaulted = false;
            strapperFaultCount = 3; // move to db as well

            return {
                success: false,
                message: `Strapper check is active but not faulted, remaining pallets to check ${strapperFaultCount}.`,
            };
        }
    }

    // strapper was triggered turning on the counter.
    //console.log(`Strapper fault is ${strapperError.value}`);
    if (tagData.value && strapperFaultCount > 0) {
        // strapper faulted we want to start the trigger to force the check
        if (!stapperFaulted) {
            createLog(
                "error",
                "dyco",
                "ocp",
                `Strapper errored triggering,  manual checks will be required for the next ${strapperFaultCount}`
            );
            stapperFaulted = true;
            // change move fault count to db....
            strapperFaultCount = 3;

            return {
                success: true,
                message: `Strapper errored triggering,  manual checks will be required for the next ${strapperFaultCount}`,
            };
        }
    }

    return {
        success: false,
        message: `Some how we made it here and just going to say we are good. :)`,
    };
};
