import { sql } from "drizzle-orm";
import { db } from "../db/db.controller.js";
import {
	type NewServerData,
	serverData,
} from "../db/schema/serverData.schema.js";
import { createLogger } from "../logger/logger.controller.js";
import { tryCatch } from "../utils/trycatch.utils.js";

const servers: NewServerData[] = [
	{
		name: "Test server 1",
		server: "USMCD1VMS036",
		plantToken: "test3",
		idAddress: "10.193.0.56",
		greatPlainsPlantCode: "00",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3",
		buildNumber: 1,
	},
	{
		name: "Test server 2",
		server: "USIOW1VMS036",
		plantToken: "test2",
		idAddress: "10.75.0.56",
		greatPlainsPlantCode: "00",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3",
		buildNumber: 1,
	},
	{
		name: "Lima",
		server: "USLIM1VMS006",
		plantToken: "uslim1",
		idAddress: "10.53.0.26", // port opened 3000 2222
		greatPlainsPlantCode: "50",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3",
		buildNumber: 1,
	},
	{
		name: "Houston",
		server: "ushou1VMS006",
		plantToken: "ushou1",
		idAddress: "10.195.0.26",
		greatPlainsPlantCode: "20",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3",
		buildNumber: 1,
	},
	{
		name: "Dayton",
		server: "usday1VMS006",
		plantToken: "usday1",
		idAddress: "10.44.0.56", // ports opened 3000 and 2222
		greatPlainsPlantCode: "80",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3",
		buildNumber: 1,
	},
	{
		name: "West Bend",
		server: "usweb1VMS006",
		plantToken: "usweb1",
		idAddress: "10.80.0.26",
		greatPlainsPlantCode: "65",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3",
		buildNumber: 1,
	},
	{
		name: "Jeff City",
		server: "usjci1VMS006",
		plantToken: "usjci",
		idAddress: "10.167.0.26",
		greatPlainsPlantCode: "40",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3",
		buildNumber: 1,
	},
	{
		name: "Sherman",
		server: "usshe1vms006",
		plantToken: "usshe1",
		idAddress: "10.205.0.26",
		greatPlainsPlantCode: "21",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3",
		buildNumber: 1,
	},
	{
		name: "McDonough",
		server: "USMCD1VMS006",
		plantToken: "usmcd1",
		idAddress: "10.193.0.26",
		greatPlainsPlantCode: "10",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3",
		buildNumber: 1,
	},
	{
		name: "St. Peters",
		server: "USSTP1VMS006",
		plantToken: "usstp1",
		idAddress: "10.37.0.26",
		greatPlainsPlantCode: "45",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3",
		buildNumber: 1,
	},
	{
		name: "Marked Tree",
		server: "USMAR1VMS006",
		plantToken: "usmar1",
		idAddress: "10.206.9.26", // 3000,2222 requested REQ0236838
		greatPlainsPlantCode: "90",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3",
		buildNumber: 1,
	},
	{
		name: "Iowa City EBM",
		server: "USIOW1VMS006",
		plantToken: "usiow1",
		idAddress: "10.75.0.26",
		greatPlainsPlantCode: "30",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3",
		buildNumber: 1,
	},
	{
		name: "Iowa City PET",
		server: "USIOW1VMS006",
		plantToken: "usiow2",
		idAddress: "10.75.0.26",
		greatPlainsPlantCode: "31",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3_2",
		buildNumber: 1,
	},
	{
		name: "Bowling Green 1",
		server: "USBOW1VMS006",
		plantToken: "usbow1",
		idAddress: "10.25.0.26", //  3000 is open REQ0236527 2222 already open
		greatPlainsPlantCode: "55",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3",
		buildNumber: 1,
	},
	{
		name: "Bethlehem",
		server: "USBET1VMS006",
		plantToken: "usbet1",
		idAddress: "10.25.0.26",
		greatPlainsPlantCode: "75",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3",
		buildNumber: 1,
	},
	{
		name: "Salt Lake City",
		server: "USSLC1VMS006",
		plantToken: "usslc1",
		idAddress: "10.202.0.26",
		greatPlainsPlantCode: "70",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3",
		buildNumber: 1,
	},
	{
		name: "Bowling Green 2",
		server: "USBOW2VMS006",
		plantToken: "usbow2",
		idAddress: "10.106.0.26",
		greatPlainsPlantCode: "56",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3",
		buildNumber: 1,
	},
	{
		name: "Kansas City",
		server: "USKSC1VMS006",
		plantToken: "usksc1",
		idAddress: "10.42.9.26",
		greatPlainsPlantCode: "85",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3",
		buildNumber: 1,
	},
	{
		name: "Florence",
		server: "USFLO1VMS006",
		plantToken: "usflo1",
		idAddress: "10.203.0.26",
		greatPlainsPlantCode: "22",
		contactEmail: "",
		contactPhone: "",
		serverLoc: "D$\\LST_V3",
		buildNumber: 1,
	},
];

// notes here for when it comes time to pull in all the server address info [test1_AlplaPROD2.0_Read].[masterData].[Plant] has everything from every server :D
export const serversChecks = async () => {
	const log = createLogger({ module: "system", subModule: "serverData" });
	const { data, error } = await tryCatch(
		db
			.insert(serverData)
			.values(servers)
			.onConflictDoUpdate({
				target: serverData.plantToken,
				set: {
					server: sql`excluded.server`,
					name: sql`excluded.name`,
					idAddress: sql`excluded."id_address"`,
					greatPlainsPlantCode: sql`excluded.great_plains_plant_code`,
					contactEmail: sql`excluded."contact_email"`,
					contactPhone: sql`excluded.contact_phone`,
					serverLoc: sql`excluded.server_loc`,
				},
			})
			.returning(),
	);

	if (error) {
		log.error(
			{ error: error },
			"There was an error when adding or updating the servers.",
		);
	}

	if (data) {
		log.info({}, "All Servers were added/updated");
	}
};

// Communication from logistic network to logisticsSupportTool (for printers and scanners)

// network justification

// scanners and printers are on dhcp
