/**
 * src/db/path.js
 *
 * Centralized, safe resolution of the SQLite database file path.
 *
 * CRITICAL PRODUCTION CONSTRAINT (as of 2026-05-28 refactor):
 * The database file used by the live production ServChan bot must never be
 * moved, renamed, or have a new file created in a different location.
 *
 * This module exists to make every part of the codebase use the exact same
 * logic when resolving the DB path:
 *   1. Respect process.env.DB_PATH if set (this is how production configures it)
 *   2. Fall back to the same default the original production index.js used
 *
 * DO NOT change the fallback path in this file without very careful coordination.
 */

import path from 'path';

// SINGLE SOURCE OF TRUTH for the DB fallback path. Do not change without
// migrating any environment that relies on the fallback. Every module that
// resolves a DB path should call getDbPath() rather than hard-coding this.
export const DEFAULT_DB_PATH = './data/webex_sc_mappings.db';

/**
 * Returns the SQLite database file path that should be used.
 *
 * Order of precedence:
 *   1. process.env.DB_PATH  (how production configures it)
 *   2. DEFAULT_DB_PATH      (matches what .env.example documents)
 */
export function getDbPath() {
  if (process.env.DB_PATH) return process.env.DB_PATH;
  return DEFAULT_DB_PATH;
}

/**
 * Returns the directory that should contain the database file.
 */
export function getDbDirectory() {
  return path.dirname(getDbPath());
}

export default {
  getDbPath,
  getDbDirectory,
};