const routes = require("express").Router();
const passport = require("passport");
const authController = require("./auth.controller");
const authValidation = require("./auth.validations");
const SchemaValidator = require("../../middlewares/schemaValidator");
const AccessValidator = require("../../middlewares/accessValidator");
const commentController = require("../comments/comment.controller");
const events_questionsController = require("../events/events_questions.controller");
const events_inscriptionsController = require("../events/events_inscriptions.controller");

//const postService = require('./post.service')(models.Post);
//const postController = require('./post.controller')(postService);

//const { ModelHandler } = require('sequelize-handlers');
//const postHandler = new ModelHandler(models.Post);

// [ADMIN] - Login
routes.post(
  "/auth",
  SchemaValidator(authValidation.LoginWinEmailInputType, true),
  AccessValidator.isRegisteredUserEmail,
  authController.login
);

//routes.get('/auth',
//    SchemaValidator(authValidation.LoginInputType, true),
//    AccessValidator.isRegisteredUserEmail,
//    authController.login2,
//);

routes.get(
  "/auth/verify",
  AccessValidator.isAdministratorUser,
  authController.adminVerify
);

// Registro de usuario a partir del usuario de Firebase y
// los datos del formulario.
routes.post(
  "/register",
  SchemaValidator(authValidation.RegisterInputType, true),
  AccessValidator.isRegisteredUserPhone,
  authController.register
);

routes.get(
  "/loginWithPhone",
  SchemaValidator(authValidation.LoginWithPhoneInputType, true),
  AccessValidator.isRegisteredUserPhone,
  authController.loginWithPhone
);

routes.post("/signup", authController.singup);

routes.get("/test_jwt", AccessValidator.isLoggedUser, function (req, res) {
  res.json({ success: "You are authenticated with JWT!", user: req.user });
});

routes.get(
  "/verify",
  SchemaValidator(authValidation.VerifyInputType, true),
  AccessValidator.isLoggedUser,
  authController.verify
);

routes.post(
  "/deleteAccount",
  SchemaValidator(authValidation.RequestDeleteAccount, true),
  AccessValidator.isAPIKeyUser,
  commentController.deleteCommentsByUser,
  events_questionsController.deleteQuestionsByUser,
  events_inscriptionsController.deleteInscriptionsByUser,
  //events_reservationsController.deleteInscriptionsByUser,
  authController.deleteUser
);


routes.post(
  "/token",
  SchemaValidator(authValidation.RequestRefreshTokenInputType, true),
  AccessValidator.isLoggedUser,
  authController.regenerateToken
);

routes.post("/token/reject", authController.rejectToken);




routes.post("/prueba", AccessValidator.isLoggedUser, function (req, res) {
  res.json({ success: "You are authenticated with JWT!", user: req.user });
});

module.exports = routes;
