/**
 * will be all the base settings so we dont have to remember to add ever new setting in these will be the defaults
 */

import { readFileSync } from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { db } from "../../../../pkg/db/db.js";
import { settings } from "../../../../pkg/db/schema/settings.js";
import { createLogger } from "../../../../pkg/logger/logger.js";
import { tryCatch } from "../../../../pkg/utils/tryCatch.js";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export const baseSettings = async () => {
	const log = createLogger({ module: "system", subModule: "base settings" });
	const settingsPath = path.resolve(__dirname, "./settings.json");

	const newSettings = JSON.parse(readFileSync(settingsPath, "utf-8"));

	const { data, error } = await tryCatch(
		db
			.insert(settings)
			.values(newSettings)
			.onConflictDoNothing()
			.returning({ name: settings.name }),
	);

	if (error) {
		log.error({ error }, "There was an error adding new settings");
	}

	if (data) {
		log.info({ newSettingsAdded: data }, "New settings added");
	}
};
