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 { query } from "../../sqlServer/prodSqlServer.js";
import { activeArticle } from "../../sqlServer/querys/dataMart/article.js";

export const getActiveAv = async (includePlantToken: boolean = false) => {
    let articles: any = [];
    try {
        const res = await query(activeArticle, "Get active articles");
        articles = res?.data;
    } catch (error) {
        articles = error;
    }

    if (includePlantToken) {
        const { data, error } = (await tryCatch(
            db.select().from(settings).where(eq(settings.name, "plantToken"))
        )) as any;

        if (error) {
            console.log("Error getting articles");
            return articles;
        }

        return articles.map((n: any) => {
            return { plantToken: data[0].value, ...n };
        });
    } else {
        return articles;
    }
};
