// src/files/storage.ts
import fs from "fs";
import path from "path";
var FILES_DIR = path.join(process.cwd(), "data", "files");
function ensureDir(dir) {
  fs.mkdirSync(dir, { recursive: true });
}
function storeFile(fileId, filename, data) {
  const dir = path.join(FILES_DIR, fileId);
  ensureDir(dir);
  const filePath = path.join(dir, filename);
  fs.writeFileSync(filePath, data);
  return path.relative(process.cwd(), filePath);
}
function readFile(storagePath) {
  const abs = path.resolve(process.cwd(), storagePath);
  return fs.readFileSync(abs);
}
function deleteFile(storagePath) {
  const abs = path.resolve(process.cwd(), storagePath);
  const dir = path.dirname(abs);
  try {
    fs.unlinkSync(abs);
    const remaining = fs.readdirSync(dir);
    if (remaining.length === 0) fs.rmdirSync(dir);
  } catch {
  }
}
function getAbsolutePath(storagePath) {
  return path.resolve(process.cwd(), storagePath);
}
function getMaxFileSize() {
  const env = process.env.FILE_SIZE_LIMIT;
  if (env) {
    const n = Number(env);
    if (Number.isFinite(n) && n > 0) return n;
  }
  return 50 * 1024 * 1024;
}

export {
  storeFile,
  readFile,
  deleteFile,
  getAbsolutePath,
  getMaxFileSize
};
