// src/utils/logPath.js
//
// Single source of truth for where log files are written.
//
// All log-writing code (structured logger, webhook payload archive, log-
// cleanup cron) should resolve their target directory via getLogDir(). This
// way you can:
//   - keep the default of ./logs (matches Docker/prod bind mounts) OR
//   - override with LOG_DIR (e.g. /var/log/servchan) in any environment
// and everything stays consistent.
//
// The directory is resolved relative to process.cwd() when the env var is
// unset — that's what `./logs` means to Node when Dockerfile's WORKDIR /app
// is in effect, or when you `npm start` from the repo root locally.

import path from 'node:path';
import fs from 'node:fs';

export const DEFAULT_LOG_DIR = './logs';

export function getLogDir() {
  const raw = process.env.LOG_DIR || DEFAULT_LOG_DIR;
  return path.resolve(raw);
}

/**
 * Make sure the log directory exists. Safe to call every write.
 * (`recursive: true` makes mkdirSync a no-op when the dir already exists.)
 */
export function ensureLogDir() {
  const dir = getLogDir();
  fs.mkdirSync(dir, { recursive: true });
  return dir;
}

export default getLogDir;
