const express = require('express');
const passport = require('passport');
const bodyParser = require('body-parser');
const services = require('../services/file');
const router = express.Router();

// JSON body parser that ONLY parses application/json content-type
// This prevents errors when binary data is sent or no body is present
const jsonParser = bodyParser.json({
  limit: '1mb',
  type: (req) => {
    const contentType = req.headers['content-type'] || '';
    return contentType.includes('application/json');
  },
});

router.get('/download', (req, res) => {
  services.downloadFile(req, res);
});

router.post('/upload/:table/:field', passport.authenticate('jwt', {session: false}), (req, res) => {
  const fileName = `${req.params.table}/${req.params.field}`;

  services.uploadFile(fileName, req, res);
});

router.post(
  '/upload-sessions/init',
  passport.authenticate('jwt', { session: false }),
  jsonParser,
  (req, res) => {
    services.initUploadSession(req, res);
  },
);

router.get(
  '/upload-sessions/:sessionId',
  passport.authenticate('jwt', { session: false }),
  (req, res) => {
    services.getUploadSession(req, res);
  },
);

// Chunk upload - NO body parser, raw stream is read directly by uploadChunk
router.put(
  '/upload-sessions/:sessionId/chunks/:chunkIndex',
  passport.authenticate('jwt', { session: false }),
  (req, res) => {
    services.uploadChunk(req, res);
  },
);

// Finalize - NO body parser needed, only uses req.params.sessionId
router.post(
  '/upload-sessions/:sessionId/finalize',
  passport.authenticate('jwt', { session: false }),
  (req, res) => {
    services.finalizeUploadSession(req, res);
  },
);

module.exports = router;
