const routes = require('express').Router();
const Joi = require('joi');

const { isAdministratorUser, isLoggedUser } = require('../../middlewares/accessValidator');
const { cacheSuccesses } = require('../../middlewares/cache');

// Uploader
// const uploader = require('../../middlewares/uploader.js');

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

const PaginateMiddleware = require('../../middlewares/paginate');
const FieldMiddleware = require('../../middlewares/fields');
const SortMiddleware = require('../../middlewares/sort');

const multimediaController = require('./multimedia.controller');

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


routes.get('/multimedias', 
    isLoggedUser, 
    cacheSuccesses('24 hours'),
    FieldMiddleware.middleware({
        invalidFields: generalInvalidFields
    }),
    PaginateMiddleware.middleware(),
    //SortMiddleware.middleware({ default: "init_available_date" }), 
    multimediaController.find({
        scopes: ['includeMultimediaFile']
    })
);

routes.get('/multimedias/:id', 
    isLoggedUser, 
    cacheSuccesses('24 hours'), 
    FieldMiddleware.middleware({
        invalidFields: generalInvalidFields
    }),
    //PaginateMiddleware.middleware(),
    //SortMiddleware.middleware({ default: "init_available_date" }), 
    multimediaController.findOne({
        scopes: ['includeMultimediaFile']
    })
);


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

const multimediasInputType = Joi.object().keys({
    entityId: Joi.string().required(),
    entityName: Joi.string().required(),
    type: Joi.string().required(),
    multimediafileId: Joi.string().required()
}); 


routes.post('/admin/multimedias/', 
    isAdministratorUser, 
    SchemaValidator(multimediasInputType, true), 
    multimediaController.create()
);

routes.put('/admin/multimedias/:id',
    isAdministratorUser,
    //SchemaValidator(multimediasInputType, true),     
    //uploader.single('file'),
    multimediaController.update()
);

routes.delete('/admin/multimedias/:id',
    isAdministratorUser,
    //SchemaValidator(multimediasInputType, true),     
    //uploader.single('file'),
    multimediaController.delete()
);

module.exports = routes;