import { readFileSync } from "node:fs";
import { join, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import type { StatusConfig } from "./types.js";
import { seedDefaultRules } from "./db.js";

const __dirname = dirname(fileURLToPath(import.meta.url));
const CONFIG_PATH = join(__dirname, "..", "data", "config.json");

let config: StatusConfig;

export function loadConfig(): StatusConfig {
  config = JSON.parse(readFileSync(CONFIG_PATH, "utf8"));
  seedDefaultRules();
  return config;
}

export function getConfig(): StatusConfig {
  if (!config) return loadConfig();
  return config;
}

export const SSH_KEY_PATH = process.env.SSH_KEY_PATH || `${process.env.HOME}/.ssh/id_ed25519`;
export const SSH_TIMEOUT = 10; // seconds
