import { Router } from 'express';
import multer from 'multer';
import path from 'path';
import crypto from 'crypto';
import { authenticateToken } from '../middlewares/auth.middleware';
import { asyncHandler } from '../middlewares/errorHandler.middleware';
import { malwareScanMiddleware } from '../middlewares/malwareScan.middleware';
import { DocumentController } from '../controllers/document.controller';
import { ensureUploadDir, UPLOAD_DIR } from '../config/storage';

// Use memory storage for GCS uploads (files will be in memory, then uploaded to GCS)
// If GCS is not configured, files will still be handled in memory and can be saved locally if needed
const storage = multer.memoryStorage();

const upload = multer({
  storage,
  limits: { fileSize: 100 * 1024 * 1024 }, // 100MB - actual limit enforced by controller using database config
});

const router = Router();
const controller = new DocumentController();

// multipart/form-data: file, requestId, optional category
// Middleware chain: auth → multer → malware scan → controller
router.post('/', authenticateToken, upload.single('file'), asyncHandler(malwareScanMiddleware), asyncHandler(controller.upload.bind(controller)));

export default router;
