const express = require('express');
const router = express.Router();

const { uploadAvatar } = require('../config/cloudinaryUpload');
const { authLimiter, forgotPasswordLimiter } = require('../config/rateLimiter');

const userController = require('../controller/user.controller');

const { asyncHandler, authUser, authAdmin } = require('../auth/checkAuth');

// Auth routes (rate limited)
router.post('/register', authLimiter, asyncHandler(userController.createUser));
router.post('/login', authLimiter, asyncHandler(userController.login));
router.post('/forgot-password', forgotPasswordLimiter, asyncHandler(userController.forgotPassword));

// Auth routes (no limit needed)
router.get('/auth', authUser, asyncHandler(userController.auth));
router.post('/logout', authUser, asyncHandler(userController.logout));
router.get('/refresh-token', asyncHandler(userController.refreshToken));
router.post('/login-google', asyncHandler(userController.loginGoogle));
router.post('/reset-password', asyncHandler(userController.resetPassword));
router.put('/update', authUser, asyncHandler(userController.updateUser));
router.post('/upload-avatar', authUser, uploadAvatar.single('avatar'), asyncHandler(userController.uploadAvatar));
router.post('/chatbot', authUser, asyncHandler(userController.chatbot));
router.get('/message-chatbot', authUser, asyncHandler(userController.getMessageChatbot));
router.post('/change-password', authUser, asyncHandler(userController.changePassword));

// Admin routes
router.get('/admin/users', authAdmin, asyncHandler(userController.getAllUser));
router.put('/admin/users/:id', authAdmin, asyncHandler(userController.updateUserAdmin));
router.delete('/admin/users/:id', authAdmin, asyncHandler(userController.deleteUser));
router.post('/admin/users/:id/add-balance', authAdmin, asyncHandler(userController.addBalance));

module.exports = router;

