import axios from "axios";
import { printerData } from "../../../../../database/schema/printers.js";
import { sql } from "drizzle-orm";
import { prodEndpointCreation } from "../../../../globalUtils/createUrl.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
import { lstAuth } from "../../../../index.js";
import { db } from "../../../../../database/dbclient.js";
import { createLog } from "../../../logger/logger.js";
import { assignedPrinters } from "../../utils/checkAssignments.js";

export const updatePrinters = async () => {
    const currentTime = new Date(Date.now());

    // get the printers from prod
    let url = await prodEndpointCreation(
        "/public/v1.0/Administration/Printers"
    );

    const { data: prodPrinters, error: prodError } = await tryCatch(
        axios.get(url, {
            headers: {
                Authorization: `Basic ${lstAuth}`,
                "Content-Type": "application/json",
            },
        })
    );

    if (prodError || prodPrinters?.data.length > 10000) {
        //console.log(prodError);
        return {
            success: false,
            message: "there was an error getting the printers.",
            data: prodError,
        };
    }

    // do the printer update into our db
    const prodPrinterInfo = prodPrinters?.data;

    for (let i = 0; i < prodPrinterInfo.length; i++) {
        const printerStuff: any = {
            humanReadableId: prodPrinterInfo[i].humanReadableId,
            name: prodPrinterInfo[i].name,
            ipAddress: prodPrinterInfo[i].ipAddress,
            port: prodPrinterInfo[i].port,
            remark: prodPrinterInfo[i].remark,
            processes: prodPrinterInfo[i].processes,
        };
        const { data, error } = await tryCatch(
            db
                .insert(printerData)
                .values(printerStuff)
                .onConflictDoUpdate({
                    target: printerData.humanReadableId,
                    set: {
                        //humanReadableId: prodPrinterInfo[i].humanReadableId,
                        name: prodPrinterInfo[i].name,
                        ipAddress: prodPrinterInfo[i].ipAddress,
                        port: prodPrinterInfo[i].port,
                        remark: prodPrinterInfo[i].remark,
                        processes: prodPrinterInfo[i].processes,
                        upd_date: sql`NOW()`,
                        //printDelay: "90", // need to remove in a couple weeks
                    },
                })
        );

        if (error) {
            createLog(
                "error",
                "lst",
                "ocp",
                `${
                    prodPrinterInfo[i].name
                } encoutered and error adding/updating ${JSON.stringify(error)}`
            );
        }
        createLog(
            "debug",
            "lst",
            "ocp",
            `${prodPrinterInfo[i].name} were just added/updated.`
        );
    }

    await assignedPrinters();

    return { success: true, message: "Printers were just added or updated." };
};
