const express = require('express');
const passport = require('passport');
const services = require('../services/file');
const router = express.Router();

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 }),
  (req, res) => {
    services.initUploadSession(req, res);
  },
);

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

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

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

module.exports = router;
