import fs from 'node:fs';
import multer from 'multer';
import { nanoid } from 'nanoid';
import { config } from '../config.js';
import { path, sanitizeFileName } from '../utils/pathUtils.js';

const incomingDir = path.resolve(config.fileLockerDir, '_incoming');
fs.mkdirSync(incomingDir, { recursive: true });

const storage = multer.diskStorage({
  destination(req, file, callback) {
    fs.mkdirSync(incomingDir, { recursive: true });
    callback(null, incomingDir);
  },
  filename(req, file, callback) {
    callback(null, `${Date.now()}-${nanoid(8)}-${sanitizeFileName(file.originalname)}`);
  }
});

// Asset uploads are staged in FileLocker before the model promotes them into the typed library tree.
export const assetUpload = multer({
  storage,
  limits: { fileSize: config.maxAssetBytes }
});
