import { eq } from "drizzle-orm";
import { db } from "../../database/dbclient.js";

import { getSettings } from "../services/server/controller/settings/getSettings.js";

// create the test server stuff
const testServers = [
    { token: "test1", port: 8940 },
    { token: "test2", port: 8941 },
    { token: "test3", port: 8942 },
];

export const prodEndpointCreation = async (endpoint: string) => {
    let url = "";
    //get the plant token
    let serverSettings = await getSettings();
    const plantToken = serverSettings?.filter((n) => n.name === "plantToken");
    // await db
    //     .select()
    //     .from(settings)
    //     .where(eq(settings.name, "plantToken"));

    // check if we are a test server
    const testServer = testServers.some(
        (server) => server.token === plantToken[0]?.value
    );
    const server = serverSettings?.filter((n) => n.name === "dbServer");
    // await db
    //     .select()
    //     .from(settings)
    //    .where(eq(settings.name, "dbServer"));

    if (testServer) {
        //filter out what testserver we are
        const test = testServers.filter((t) => t.token === plantToken[0].value);
        // "https://usmcd1vms036.alpla.net:8942/application/public/v1.0/DemandManagement/ORDERS"
        // "https://usmcd1vms036.alpla.net:8492/application/public/v1.0/DemandManagement/ORDERS"
        url = `https://${server[0]?.value}.alpla.net:${test[0]?.port}/application${endpoint}`;
        return url;
    } else {
        url = `https://${plantToken[0]?.value}prod.alpla.net/application${endpoint}`;
        return url;
    }
};
