const routes = require('express').Router();
const config = require('../../config');
// Uploader
const cdnUpload = require('../../middlewares/uploader.js');
const { isAdministratorUser, isLoggedUser } = require('../../middlewares/accessValidator');
const { cacheSuccesses } = require('../../middlewares/cache');

const SchemaValidator = require('../../middlewares/schemaValidator');

const PaginateMiddleware = require('../../middlewares/paginate');
const FieldMiddleware = require('../../middlewares/fields');
const SortMiddleware = require('../../middlewares/sort');
const multimediaFileController = require('./multimedia_file.controller');
const { multimediaFilesInputType } = require('./multimedia_file.validations');

const generalInvalidFields = [
    'userId', 'createdAt', 'updatedAt',
];

routes.get('/multimediafiles', cacheSuccesses('24 hours'),
    FieldMiddleware.middleware({
        invalidFields: generalInvalidFields
    }),
    PaginateMiddleware.middleware(),
    SortMiddleware.middleware({ default: "createdAt" }), 
    multimediaFileController.find({
        scopes: ['includeMultimedias']
    })
);

routes.get('/multimediafiles/:id', cacheSuccesses('24 hours'), 
    FieldMiddleware.middleware({
        invalidFields: generalInvalidFields
    }),
    //SortMiddleware.middleware({ default: "init_available_date" }), 
    multimediaFileController.findOne({
        scopes: ['includeMultimedias']
    })
);

/*********************************************************************************************************
 *  ADMINISTRACIÓN
 *********************************************************************************************************
 */

// Nuevo
routes.post('/admin/multimediafiles/',
    isAdministratorUser,
    SchemaValidator(multimediaFilesInputType, true),
    cdnUpload().single('file'),
    (req, res, next) => {
        console.log('POST ****************************');
        console.log(req.file, req.data);
        if (req.file) {
            console.log('--->', req.file.path);
            req.body.url = req.file.path.replace(config.uploads.path, '');
            console.log('--->', req.body.url);
            req.body.provider = 'cdn';
            req.body.class = 'picture';
        }
        next();
    },
    multimediaFileController.create()
);

// Modificación
routes.put('/admin/multimediafiles/:id',
    isAdministratorUser,
    SchemaValidator(multimediaFilesInputType, true),
    cdnUpload().single('file'),
    (req, res, next) => {
        console.log('PUT ****************************');
        console.log(req.file, req.data, req.body);
        if (req.file) {
            req.body.url = req.file.path.replace(config.uploads.path, '');
            req.body.provider = 'cdn';
            req.body.class = 'picture';
        }
        next();
    },
    multimediaFileController.update()
);

// Borrado
routes.delete('/admin/multimediafiles/:id',
    isAdministratorUser,
    multimediaFileController.delete()
);


module.exports = routes;