const express = require('express');
const authService = require('../services/authService');
const {authenticate} = require('../middleware/auth');
const {asyncHandler} = require('../utils/asyncHandler');
const {success} = require('../utils/apiResponse');

const router = express.Router();

router.post('/login', asyncHandler(async (req, res) => {
  console.log('Login request body:', req.body); // Log the request body for debugging
  const session = await authService.login(req.body, req);
  return success(res, session, 'Login successful');
}));

router.post('/refresh', asyncHandler(async (req, res) => {
  const session = await authService.refresh(req.body.refreshToken, req);
  return success(res, session, 'Token refreshed');
}));

router.post('/logout', authenticate, asyncHandler(async (req, res) => {
  await authService.logout(req.user.id, req.body.refreshToken);
  return success(res, {}, 'Logout successful');
}));

router.post('/forgot-password', (req, res) => success(res, {}, 'Forgot password flow is not enabled yet'));

module.exports = router;
