import { resolve } from "node:path";

const defaultDbPath = resolve(process.cwd(), "data", "mock-task-manager.sqlite");

function parsePort(value?: string): number {
  const port = Number.parseInt(value ?? "3010", 10);
  if (!Number.isInteger(port) || port <= 0) {
    throw new Error(`Invalid PORT value: ${value}`);
  }

  return port;
}

export function getConfig(env: NodeJS.ProcessEnv = process.env) {
  return {
    port: parsePort(env.PORT),
    dbPath: env.DB_PATH?.trim() || defaultDbPath,
  };
}
