import fs from 'fs';

/**
 * Read version from file. Order:
 *   1. APP_VERSION_FILE env var (e.g. /data/VERSION from mounted volume)
 *   2. /app/VERSION (built into Docker image)
 *   3. Fallback hardcoded default
 */
function readVersionFromFile(): string {
  const paths = [
    process.env.APP_VERSION_FILE,       // from env
    '/app/VERSION',                      // built-in fallback
  ].filter(Boolean) as string[];

  for (const p of paths) {
    try {
      const content = fs.readFileSync(p, 'utf-8').trim();
      if (content) return content;
    } catch {
      // file doesn't exist, try next
    }
  }

  return '0.0.0';  // ultimate fallback — should never happen
}

export const VERSION = readVersionFromFile();
