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

const { cacheSuccesses } = require('../../middlewares/cache');
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 entityValidation = require('./entity.validations');
const entityController = require('./entity.controller');


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


routes.get('/entities',
    isLoggedUser,
    cacheSuccesses('24 hours'),
    FieldMiddleware.middleware({
        invalidFields: generalInvalidFields
    }),
    PaginateMiddleware.middleware(),
    SortMiddleware.middleware({ default: "name" }),
    entityController.find({
        scopes: ['defaultScope'],
    }),
);


routes.get ('/entities/colleges',
    cacheSuccesses('24 hours'),
//    PaginateMiddleware.middleware(),
    FieldMiddleware.middleware({
        invalidFields: generalInvalidFields
    }),
    SortMiddleware.middleware({ default: "name" }),    
    entityController.find({
        scopes: ['defaultScope', 'onlyColleges'],
    }),
);

routes.get ('/entities/partners', 
    cacheSuccesses('24 hours'),
//    PaginateMiddleware.middleware(),
    FieldMiddleware.middleware({
        invalidFields: generalInvalidFields
    }),
    SortMiddleware.middleware({ default: "name" }),
    entityController.find({
        scopes: ['defaultScope', 'onlyPartners'],
    }),
);


/********************************************************************************************************
 *  ADMINISTRACIÓN
 *********************************************************************************************************
 */
routes.get('/admin/entities/partners/:id', 
    //isAdministratorUser,
    entityController.findOne(),
);

module.exports = routes;