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

const newSettings: NewSetting[] = [
	// feature settings
	{
		name: "opendock_sync",
		value: "15",
		active: false,
		description: "Dock Scheduling system",
		moduleName: "opendock",
		settingType: "feature",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "ocp",
		value: "1",
		active: false,
		description: "One click print",
		moduleName: "ocp",
		settingType: "feature",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "ocme",
		value: "0",
		active: false,
		description: "Dayton Agv system",
		moduleName: "ocme",
		settingType: "feature",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "demandManagement",
		value: "1",
		active: false,
		description: "Fake EDI System",
		moduleName: "demandManagement",
		settingType: "feature",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "qualityRequest",
		value: "0",
		active: false,
		description: "Quality System",
		moduleName: "qualityRequest",
		settingType: "feature",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "tms",
		value: "0",
		active: false,
		description: "Transport system integration",
		moduleName: "tms",
		settingType: "feature",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "purchaseMonitor",
		value: "5",
		active: true,
		description: "Monitors alpla purchase fo all changes",
		moduleName: "purchase",
		settingType: "feature",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "mobile",
		value: "0",
		active: false,
		description: "LST Android Mobile app",
		moduleName: "mobile",
		settingType: "feature",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "dockDoorScanning",
		value: "0",
		active: false,
		description: "dock door scanning",
		moduleName: "dockDoorScanning",
		settingType: "feature",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "eom",
		value: "0",
		active: true,
		description: "Eom processes to capture data",
		moduleName: "eom",
		settingType: "feature",
		roles: ["admin"],
		seedVersion: 1,
	},

	// standard settings
	{
		name: "stagingWarehouse",
		value: "30218",
		active: true,
		description:
			"The warehouse we will use for staging, validation that we did our prechecks if required",
		moduleName: "dockDoorScanning",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "precheck",
		value: "5",
		active: false,
		description:
			"Precheck is required, the value is in minute, 5 min should be 5",
		moduleName: "dockDoorScanning",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "prolinkCheck",
		value: "1",
		active: true,
		description:
			"Will prolink be considered to check if matches, mainly used in plants that do not fully utilize prolink + ocp",
		moduleName: "ocp",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "bookin",
		value: "1",
		active: true,
		description: "Will we book in the labels after they are printed.",
		moduleName: "ocp",
		settingType: "standard",
		roles: ["admin", "supervisor"],
		seedVersion: 2,
	},
	{
		name: "printDelay",
		value: "90",
		active: true,
		description: "The default time between label printing",
		moduleName: "ocp",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "dualPrinting",
		value: "0",
		active: true,
		description: "Are we producing on 2 lines that pack into the 1 packer",
		moduleName: "ocp",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "fifoCheck",
		value: "45",
		active: true,
		description:
			"This check is used to do a more near fifo check when pulling pallets for the agv's",
		moduleName: "ocme",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "dayCheck",
		value: "3",
		active: true,
		description: "how many days +/- to check for shipments in alplaprod",
		moduleName: "ocme",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "maxLotsPerTruck",
		value: "3",
		active: true,
		description:
			"What is the maximum amount of lots that can be pulled for a truck this",
		moduleName: "ocme",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "monitorAddress",
		value: "8",
		active: true,
		description:
			"What address(2) are we monitoring for lot restrictions. multiple addresses can be used but should be separated by a comma ,",
		moduleName: "ocme",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "ocmeCycleCount",
		value: "0",
		active: true,
		description:
			"Are you enabling the system count page? meaning that we will allow a 'manual cycle count' vs full auto",
		moduleName: "ocme",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "inhouseDelivery",
		value: "0",
		active: true,
		description:
			"This is for in-house plants that deliver direct to the customer, note if book-in is off this will be ignored ",
		moduleName: "ocp",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "dycoConnect",
		value: "0",
		active: true,
		description: "Connection to the dyco in dayton for the labeling process",
		moduleName: "ocp",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "dycoPrint",
		value: "0",
		active: true,
		description:
			"This tells us we want to use the dyco system to print the labels",
		moduleName: "ocp",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "strapperCheck",
		value: "0",
		active: true,
		description:
			"Strapper alarm check this is more used in tandem with dycoPrint setting",
		moduleName: "ocp",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "rfid_ocp",
		value: "0",
		active: true,
		description:
			"Enable the rfid pallet mgt system this replaces dycoPrint but can be overridden by dycoPrint",
		moduleName: "ocp",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "ocpCycleDelay",
		value: "10",
		active: true,
		description:
			"How long is the delay to fire off the printer check status, Defaulting to 10 seconds",
		moduleName: "ocp",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "pNgAddress",
		value: "139",
		active: true,
		description:
			"This is the P&G address that is used when uploading the new forecast from P&G",
		moduleName: "demandManagement",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "zechetti_1",
		value: "0",
		active: true,
		description:
			"Active the Zechetti 1 process, to print from and no longer utilize the pc",
		moduleName: "ocp",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "zechetti_2",
		value: "0",
		active: true,
		description:
			"Active the Zechetti 2 process, to print from and no longer utilize the pc",
		moduleName: "ocp",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "checkColor",
		value: "0",
		active: true,
		description:
			"During the material check are we going to check the color has enough provided to create the next pallet",
		moduleName: "ocp",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "checkPKG",
		value: "0",
		active: true,
		description:
			"During the material check are we going to check the packaging has enough provided to create the next pallet",
		moduleName: "ocp",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "lotPrintDelay",
		value: "0",
		active: true,
		description:
			"Override ride the printer printer delay setting, by default if this is on all printers are controlled by this.",
		moduleName: "ocp",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "laneCheck",
		value: "0",
		active: false,
		description:
			"Allows the driver to scan a lane and see what is in the lane and details about each pallet.",
		moduleName: "mobile",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "dockScan",
		value: "0",
		active: false,
		description:
			"Enables dock door scanning, must have a dock scanner setup for this to work.",
		moduleName: "mobile",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "cycleCounting",
		value: "0",
		active: false,
		description: "Enables a cycle count to be triggered from the scanner.",
		moduleName: "mobile",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "scannerIds",
		value: "10",
		active: false,
		description:
			"How many scanners ids are setup for this, there should be a lst_scanner instance created.",
		moduleName: "mobile",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "defaultLoadType",
		value: "drop",
		active: false,
		description:
			"What is the default load type we will use for creating new apt: drop or live are the current options.",
		moduleName: "opendock",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
	{
		name: "defaultOrderTime",
		value: "8",
		active: true,
		description:
			"What time will we be using as our default time for orders that we do not pass a time over.",
		moduleName: "demandManagement",
		settingType: "standard",
		roles: ["admin"],
		seedVersion: 1,
	},
];

export const baseSettingValidationCheck = async () => {
	const log = createLogger({ module: "system", subModule: "settings" });
	const { data, error } = await tryCatch(
		db
			.insert(settings)
			.values(newSettings)
			.onConflictDoUpdate({
				target: settings.name,
				set: {
					roles: sql`excluded.roles`,
					description: sql`excluded.description`,
					moduleName: sql`excluded."moduleName"`,
					settingType: sql`excluded."settingType"`,
					seedVersion: sql`excluded.seed_version`,
					upd_user: "LST_System",
					upd_date: sql`now()`,
				},
				where: sql`
                    settings.seed_version IS NULL
                    OR settings.seed_version < excluded.seed_version
                `,
			})
			.returning(),
	);

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

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