// src/routes/users.routes.js
const express = require('express');
const rateLimit = require('express-rate-limit');
const usersService = require('../services/users.service');

const router = express.Router();

const authLimiter = rateLimit({
  windowMs: 60 * 1000,
  max: 10,
  standardHeaders: true,
  legacyHeaders: false,
});

router.post('/login', authLimiter, async (req, res, next) => {
  try {
    const { username, password } = req.body;
    const data = await usersService.login(username, password);
    res.json({ success: true, data });
  } catch (err) {
    next(err);
  }
});

router.post('/register', authLimiter, async (req, res, next) => {
  try {
    const { username, password } = req.body;
    const data = await usersService.register(username, password);
    res.status(201).json({ success: true, data });
  } catch (err) {
    next(err);
  }
});

module.exports = router;
