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

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

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

//const entityValidation = require('./entity.validations');
const speakerController = require("./speaker.controller");

const generalInvalidFields = ["userId", "createdAt", "state"];

// Todos los ponentes
routes.get(
  "/speakers",
  isOptionalUser,
  cacheSuccesses("1 minute"),
  FieldMiddleware.middleware({
    invalidFields: generalInvalidFields,
  }),
  PaginateMiddleware.middleware(),
  SortMiddleware.middleware({ default: "name" }),
  speakerController.find({
    scopes: ["defaultScope", "onlyPublished", "includeValues", "includeMultimedias"],
  })
);

// Ponentes destacados
routes.get(
  "/speakers/featured",
  isOptionalUser,
  cacheSuccesses("1 minute"),
  FieldMiddleware.middleware({
    invalidFields: generalInvalidFields,
  }),
  PaginateMiddleware.middleware(),
  //SortMiddleware.middleware({ default: "name" }),
  speakerController.find({
    scopes: ["defaultScope", "onlyPublished", "includeValues", "featured", "includeMultimedias"],
  })
);

// Ponentes más recientes
routes.get(
  "/speakers/last",
  isOptionalUser,
  cacheSuccesses("1 minute"),
  FieldMiddleware.middleware({
    invalidFields: ["userId"],
  }),
  PaginateMiddleware.middleware(),
  SortMiddleware.middleware({ default: "-createdAt" }),
  speakerController.find({
    scopes: ["defaultScope", "onlyPublished", "last", "includeMultimedias"],
  })
);

// Un ponente
routes.get(
  "/speakers/:id",
  isOptionalUser,
  cacheSuccesses("24 hours"),
  FieldMiddleware.middleware({
    invalidFields: generalInvalidFields,
  }),
  speakerController.findOne({
    scopes: ["defaultScope", "onlyPublished", "includeValues", "includeMultimedias", "includeComments"],
  })
);

// Dar ponentes similares a uno dado
routes.get(
  "/speakers/:id/similar",
  isOptionalUser,
  cacheSuccesses("24 hours"),
  /*FieldMiddleware.middleware({
        invalidFields: ['createdAt']
    }),*/
  PaginateMiddleware.middleware(),
  SortMiddleware.middleware({ default: "name" }),
  speakerController.findSimilar()
);

// Listar las preguntas hechas a un ponente
routes.get(
  "/speakers/:id/:association",
  isLoggedUser,
  /*FieldMiddleware.middleware({
        invalidFields: ['createdAt']
    }),*/
  PaginateMiddleware.middleware(),
  SortMiddleware.middleware({ default: "-createdAt" }),
  speakerController.find()
);

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

// Todos los ponentes
routes.get(
  "/admin/speakers",
  isAdministratorUser,
  SortMiddleware.middleware({ default: "name" }),
  speakerController.find({
    scopes: ["defaultScope", "includeValues", "includeMultimedias"],
  })
);

// Un ponente
routes.get(
  "/admin/speakers/:id",
  isAdministratorUser,
  speakerController.findOne({
    scopes: ["defaultScope", "includeValues", "includeMultimedias"],
  })
);

// Comentarios de un ponente
routes.get(
  "/admin/speakers/:id/comments",
  isAdministratorUser,
  (req, res, next) => {
    req.params.association = "comments";
    next();
  },
  speakerController.find()
);

routes.get(
  "/admin/speakers/:id/values",
  isAdministratorUser,
  (req, res, next) => {
    req.params.association = "values";
    next();
  },
  speakerController.find()
);

routes.get(
  "/admin/speakers/:id/questions",
  isAdministratorUser,
  (req, res, next) => {
    req.params.association = "questions";
    next();
  },
  speakerController.find()
);

routes.get(
  "/admin/speakers/:id/multimedias",
  isAdministratorUser,
  (req, res, next) => {
    req.params.association = "multimedias";
    next();
  },
  speakerController.find()
);

// Nuevo ponente
routes.post("/admin/speakers/", isAdministratorUser, speakerController.create());

// Modificar ponente
routes.put("/admin/speakers/:id", isAdministratorUser, speakerController.update());

// Borrar ponente
routes.delete("/admin/speakers/:id", isAdministratorUser, speakerController.delete());

module.exports = routes;
