import express from 'express';
import passport from 'passport';
import { wrapAsync } from '@/api/http/request';
import * as auth from '@/api/controllers/auth.controller';

const router = express.Router();

const authenticated = passport.authenticate('jwt', { session: false });

router.post('/signin/local', wrapAsync(auth.signinLocal));
router.post('/refresh', wrapAsync(auth.refresh));
router.post('/signout', wrapAsync(auth.signout));
router.get('/me', authenticated, wrapAsync(auth.me));
router.put('/password-reset', wrapAsync(auth.passwordReset));
router.put('/password-update', authenticated, wrapAsync(auth.passwordUpdate));
router.post(
  '/send-email-address-verification-email',
  authenticated,
  wrapAsync(auth.sendEmailVerification),
);
router.post('/send-password-reset-email', wrapAsync(auth.sendPasswordResetEmail));
router.post('/signup', wrapAsync(auth.signup));
router.put('/profile', authenticated, wrapAsync(auth.updateProfile));
router.put('/verify-email', wrapAsync(auth.verifyEmail));
router.get('/email-configured', auth.emailConfigured);

router.get('/signin/google', auth.googleSignin);
router.get(
  '/signin/google/callback',
  passport.authenticate('google', { failureRedirect: '/login', session: false }),
  wrapAsync(auth.googleCallback),
);
router.get('/signin/microsoft', auth.microsoftSignin);
router.get(
  '/signin/microsoft/callback',
  passport.authenticate('microsoft', {
    failureRedirect: '/login',
    session: false,
  }),
  wrapAsync(auth.microsoftCallback),
);

export default router;
