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

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

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

const commentValidation = require('./comment.validations');
const commentController = require('./comment.controller');


const generalInvalidFields = [
];


routes.get('/comments',
    isLoggedUser,
    FieldMiddleware.middleware({
        invalidFields: generalInvalidFields
    }),
    PaginateMiddleware.middleware(),
    SortMiddleware.middleware({ default: "createdAt" }),
    commentController.find({
        scopes: ['defaultScope'],
    }),
);

routes.get('/comments/events',
    isLoggedUser,
    FieldMiddleware.middleware({
        invalidFields: generalInvalidFields
    }),
    PaginateMiddleware.middleware(),
    SortMiddleware.middleware({ default: "createdAt" }),
    commentController.find({
        scopes: ['defaultScope', 'onlyEvents'],
    }),
);

routes.get('/comments/speakers',
    isLoggedUser,
    FieldMiddleware.middleware({
        invalidFields: generalInvalidFields
    }),
    PaginateMiddleware.middleware(),
    SortMiddleware.middleware({ default: "createdAt" }),
    commentController.find({
        scopes: ['defaultScope', 'onlySpeakers'],
    }),
);

//Crear un comentario
routes.post('/comments', 
isLoggedUser, 
SchemaValidator(commentValidation.CommentInputType, true), 
commentController.create());

//Modificar un comentario
//routes.put('/comments/:id', isLoggedUser, SchemaValidator(commentValidation.CommentInputType, true), commentController.update());

/*********************************************************************************************************
 *  ADMINISTRACIÓN
 *********************************************************************************************************
 */
routes.get('/admin/comments',
    isAdministratorUser,
    //    PaginateMiddleware.middleware(),
    SortMiddleware.middleware({ default: "-createdAt" }),
    commentController.find({
        scopes: ['defaultScope'],
    }),
);


routes.get('/admin/comments/events/:entityId',
    isAdministratorUser,
//    PaginateMiddleware.middleware(),
    SortMiddleware.middleware({ default: "createdAt" }),
    commentController.find({
        scopes: ['defaultScope', 'onlyEvents'],
    }),
);

routes.get('/admin/comments/posts/:entityId',
    isAdministratorUser,
    //    PaginateMiddleware.middleware(),
    SortMiddleware.middleware({ default: "createdAt" }),
    commentController.find({
        scopes: ['defaultScope', 'onlyPosts'],
    }),
);

routes.get('/admin/comments/speakers/:entityId',
    isAdministratorUser,
    //    PaginateMiddleware.middleware(),
    SortMiddleware.middleware({ default: "createdAt" }),
    commentController.find({
        scopes: ['defaultScope', 'onlySpeakers'],
    }),
);

// Borrado
routes.delete('/admin/comments/:id',
    isAdministratorUser,
    commentController.delete()
);

module.exports = routes;