const express = require('express');
const { registerUser, deleteAccount, login, googleLogin, googleRegister, sendVerificationOTP, verifyEmailOTP, forgotPassword, resetPassword } = require('../controllers/auth');
const { validateUser } = require('../validations/userValidation');
const { validationResult } = require('express-validator');
const passport = require('passport');
require('../utils/google');

const router = express.Router();
router.use(passport.initialize());

// Middleware to handle validation errors
const validateRequest = (req, res, next) => {
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(400).json({ errors: errors.errors[0].msg });
  }
  next();
};

// User registration route
router.post('/register', validateUser, validateRequest, registerUser);
router.post('/login', login);

router.get(
  '/google/login',
  passport.authenticate('google-login', {
    scope: ['profile', 'email'],
    session: false,
  })
);

router.get(
  '/google/register',
  passport.authenticate('google-register', {
    scope: ['profile', 'email'],
    session: false,
  })
);

router.get(
  '/google/register/callback',
  googleRegister
);

router.get(
  '/google/login/callback',
  googleLogin
);

router.post('/send-verification-otp', sendVerificationOTP);

router.post('/verify-email', verifyEmailOTP);

router.post('/forgot-password', forgotPassword);

router.post('/reset-password', resetPassword);

router.delete('/delete-account', deleteAccount);

module.exports = router;
