const express = require('express');
const { body } = require('express-validator');
const validate = require('../middleware/validate');
const { authenticate } = require('../middleware/auth');
const AuthController = require('../controllers/AuthController');

const router = express.Router();

router.post(
  '/login',
  [
    body('email').isEmail().withMessage('Valid email required'),
    body('password').notEmpty().withMessage('Password required'),
    validate,
  ],
  AuthController.login
);

router.post(
  '/register',
  [
    body('username').notEmpty().withMessage('Username required'),
    body('email').isEmail().withMessage('Valid email required'),
    body('password').isLength({ min: 6 }).withMessage('Password must be at least 6 characters'),
    body('full_name').notEmpty().withMessage('Full name required'),
    validate,
  ],
  AuthController.register
);

router.get('/me', authenticate, AuthController.me);

router.put('/me', authenticate, AuthController.updateProfile);

router.post(
  '/forgot-password',
  [body('email').isEmail().withMessage('Valid email required'), validate],
  AuthController.forgotPassword
);

router.post(
  '/verify-reset-code',
  [
    body('email').isEmail().withMessage('Valid email required'),
    body('code').notEmpty().withMessage('Verification code required'),
    validate,
  ],
  AuthController.verifyResetCode
);

router.post(
  '/reset-password',
  [
    body('token').notEmpty().withMessage('Reset token required'),
    body('password').isLength({ min: 6 }).withMessage('Password must be at least 6 characters'),
    validate,
  ],
  AuthController.resetPassword
);

module.exports = router;
