const express = require('express');
const passport = require('passport');
const services = require('../services/file');
const { requireStaff } = require('../security/coachingAuthorization');
const router = express.Router();
const authenticate = passport.authenticate('jwt', {session: false});

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

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

  return services.uploadLocal(fileName, {
    entity: null,
    maxFileSize: 10 * 1024 * 1024,
    folderIncludesAuthenticationUid: false,
  })(req, res, next);
});

module.exports = router;
