/**
 * While in production we will monitor the readers if we have not gotten a heartbeat in the last 5 min we will send a reboot command along with an email.
 */

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

export const newHeartBeat = async (reader: string) => {
    /**
     * When a heat beat is sent over for a reader we want to update the reader.
     */

    try {
        const heatBeat = await db
            .update(rfidReaders)
            .set({ lastHeartBeat: sql`NOW()` })
            .where(eq(rfidReaders.reader, reader));
        createLog(
            "debug",
            "rfid",
            "rfid",
            `${reader} just updated its heatBeat.`
        );
        return {
            success: true,
            message: `${reader} just updated its heatBeat.`,
        };
    } catch (error) {
        createLog(
            "error",
            "rfid",
            "rfid",
            `${reader} encountered an error while updating the heatbeat, ${error}`
        );
        return {
            success: false,
            message: `${reader} encountered an error while updating the heatbeat, ${error}`,
        };
    }
};

export const badRead = async (reader: string) => {
    /**
     * When we have a bad read we want to make sure the reader shows this was well.
     */
    try {
        const badRead = await db
            .update(rfidReaders)
            .set({
                lastTrigger: sql`NOW()`,
                lastTriggerGood: false,
                lastTagScanned: null,
                badReads: sql`${rfidReaders.badReads} + 1`,
            })
            .where(eq(rfidReaders.reader, reader));
        createLog(
            "info",
            "rfid",
            "rfid",
            `${reader} just Triggered a bad read.`
        );
        return {
            success: true,
            message: `${reader} just Triggered a bad read.`,
        };
    } catch (error) {
        createLog(
            "error",
            "rfid",
            "rfid",
            `${reader} encountered an error while updating the heatbeat, ${error}`
        );
        return {
            success: false,
            message: `${reader} encountered an error while updating the heatbeat, ${error}`,
        };
    }
};

export const resetRatios = async (reader: string) => {
    const { error } = await tryCatch(
        db
            .update(rfidReaders)
            .set({
                goodReads: 0,
                badReads: 0,
            })
            .where(eq(rfidReaders.reader, reader))
    );

    if (error) {
        createLog(
            "error",
            "rfid",
            "rfid",
            `${reader} encountered an error resetting the data.`
        );
        return {
            success: false,
            message: `${reader} encountered an error resetting the data.`,
        };
    }
    createLog("info", "rfid", "rfid", `${reader} just had the tag data reset.`);
    return {
        success: true,
        message: `${reader} just had the tag data reset.`,
    };
};

export const goodRead = async (reader: string) => {
    /**
     * When we have a bad read we want to make sure the reader shows this was well.
     */
    try {
        const goodRead = await db
            .update(rfidReaders)
            .set({
                lastTrigger: sql`NOW()`,
                lastTriggerGood: true,
                goodReads: sql`${rfidReaders.goodReads} + 1`,
            })
            .where(eq(rfidReaders.reader, reader));
        createLog(
            "info",
            "rfid",
            "rfid",
            `${reader} just Triggered a good read.`
        );
        return {
            success: true,
            message: `${reader} just Triggered a good read.`,
        };
    } catch (error) {
        createLog(
            "error",
            "rfid",
            "rfid",
            `${reader} encountered an error while updating the heatbeat, ${error}`
        );
        return {
            success: false,
            message: `${reader} encountered an error while updating the heatbeat, ${error}`,
        };
    }
};
