const router = require('express').Router();
const { register, login, me, updateProfile } = require('../controllers/authController');
const { authMiddleware } = require('../middleware/auth');
const rateLimit = require('../middleware/rateLimit');
const validate  = require('../middleware/validate');

const loginLimiter    = rateLimit({ windowMs: 60_000, max: 10, message: 'Слишком много попыток входа, подождите минуту' });
const registerLimiter = rateLimit({ windowMs: 60_000, max: 5,  message: 'Слишком много регистраций, подождите минуту' });
const profileLimiter  = rateLimit({ windowMs: 60_000, max: 10, message: 'Слишком много запросов, подождите минуту' });

const registerSchema = { body: {
  email:    { type: 'string', required: true, maxLen: 255, match: /^[^\s@]+@[^\s@]+\.[^\s@]+$/ },
  password: { type: 'string', required: true, minLen: 6, maxLen: 128 },
  name:     { type: 'string', required: true, minLen: 1, maxLen: 100 },
}};
const loginSchema = { body: {
  email:    { type: 'string', required: true, maxLen: 255 },
  password: { type: 'string', required: true, minLen: 1, maxLen: 128 },
}};
const profileSchema = { body: {
  name:            { type: 'string', minLen: 1, maxLen: 100 },
  newPassword:     { type: 'string', minLen: 6, maxLen: 128 },
  currentPassword: { type: 'string', maxLen: 128 },
}};

router.post('/register',  registerLimiter, validate(registerSchema), register);
router.post('/login',     loginLimiter,    validate(loginSchema),    login);
router.get('/me',         authMiddleware, me);
router.get('/profile',    authMiddleware, me);
router.patch('/profile',  authMiddleware, profileLimiter, validate(profileSchema), updateProfile);

module.exports = router;
