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 entityTypeController = require("./entity_type.controller");

const generalInvalidFields = ["state", "contact_person", "contact_email", "createdAt", "updatedAt"];

routes.get(
  "/entities/colleges",
  cacheSuccesses("1 hours"),
  //    PaginateMiddleware.middleware(),
  FieldMiddleware.middleware({
    invalidFields: generalInvalidFields,
  }),
  SortMiddleware.middleware({ default: "name" }),
  (req, res, next) => {
    if (req.query && req.query.country) {
      if (!["ES", "MX"].includes(req.query.country)) {
        delete req.query["country"];
      }
    }
    next();
  },
  entityController.find({
    scopes: ["defaultScope", "onlyColleges"],
  })
);

routes.get(
  "/entities/partners",
  cacheSuccesses("1 hours"),
  //    PaginateMiddleware.middleware(),
  FieldMiddleware.middleware({
    invalidFields: generalInvalidFields,
  }),
  SortMiddleware.middleware({ default: "name" }),
  (req, res, next) => {
    if (req.query && req.query.country) {
      if (!["ES", "MX"].includes(req.query.country)) {
        delete req.query["country"];
      }
    }
    next();
  },
  entityController.find({
    scopes: ["defaultScope", "onlyPartners"],
  })
);

/********************************************************************************************************
 *  ADMINISTRACIÓN
 *********************************************************************************************************
 */
routes.get(
  "/admin/entities",
  isAdministratorUser,
  //    FieldMiddleware.middleware({
  //        invalidFields: generalInvalidFields
  //    }),
  //    PaginateMiddleware.middleware(),
  SortMiddleware.middleware({ default: "name" }),
  entityController.find()
);

routes.get(
  "/admin/entities/partners/:id",
  //isAdministratorUser,
  entityController.findOne()
);

routes.get(
  "/admin/entities/:id",
  //isAdministratorUser,
  entityController.findOne({
    scopes: ["withEntityTypes"],
  })
);

routes.get(
  "/admin/entitytypes/",
  //isAdministratorUser,
  entityTypeController.find()
);

routes.put(
  "/admin/entities/:id",
  isAdministratorUser,
  //SchemaValidator(eventValidation.ReservationInputType, true),
  entityController.update()
);

routes.post(
  "/admin/entities",
  isAdministratorUser,
  //SchemaValidator(eventValidation.ReservationInputType, true),
  entityController.create()
);

routes.post(
  "/admin/entities/:id",
  isAdministratorUser,
  //SchemaValidator(eventValidation.ReservationInputType, true),
  entityController.delete()
);

module.exports = routes;
