import express from 'express';
import { isAuthenticated } from '../../keycloak.js';
import {
  forgotPasswordRouteHandler,
  loginRouteHandler,
  loginCallbackRouteHandler,
  userRouteHandler,
  logoutRouteHandler,
  refreshTokenRouteHandler,
  loginTokenRouteHandler,
} from '../../services/misc/auth.js';

const router = express.Router();

router.get('/:redirectType/login', async (req, res) => {
  loginRouteHandler(req, res, req.params.redirectType);
});

router.get('/:redirectType/callback', async (req, res) => {
  loginCallbackRouteHandler(req, res, req.params.redirectType);
});

router.get('/:redirectType/token', async (req, res) => {
  loginTokenRouteHandler(req, res, req.params.redirectType);
});

router.get('/refresh', async (req, res) => {
  refreshTokenRouteHandler(req, res);
});

router.get('/user', isAuthenticated, async (req, res) => {
  userRouteHandler(req, res);
});

router.get('/logout', (req, res) => {
  logoutRouteHandler(req, res);
});

router.post('/password-forgot', async (req, res) => {
  const { email } = req.body;
  await forgotPasswordRouteHandler(req, res, email);
});

export default router;
