import { existsSync } from 'fs';
import { resolve } from 'path';

export function getUploadRoot() {
  if (process.env.UPLOAD_DIR?.trim()) {
    return resolve(process.env.UPLOAD_DIR.trim());
  }

  const candidates = [
    resolve(process.cwd(), '..', '..', 'uploads'),
    resolve(process.cwd(), 'uploads'),
    resolve(__dirname, '..', '..', 'uploads'),
  ];

  return candidates.find((p) => existsSync(p)) ?? candidates[0];
}
