/**
 * Authentication Routes
 */

const express = require("express");
const router = express.Router();
const { body } = require("express-validator");
const { validate } = require("../middleware/validationMiddleware");
const { authenticate } = require("../middleware/authMiddleware");
const authController = require("../controllers/authController");

// Login validation
const loginValidation = [
  body("username").notEmpty().withMessage("Username or email is required"),
  body("password").notEmpty().withMessage("Password is required"),
];

// Change password validation
const changePasswordValidation = [
  body("currentPassword")
    .notEmpty()
    .withMessage("Current password is required"),
  body("newPassword")
    .isLength({ min: 8 })
    .withMessage("New password must be at least 8 characters")
    .matches(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)/)
    .withMessage("Password must contain uppercase, lowercase and number"),
];

// Routes
router.post("/login", loginValidation, validate, authController.login);
router.get("/profile", authenticate, authController.getProfile);
router.post(
  "/change-password",
  authenticate,
  changePasswordValidation,
  validate,
  authController.changePassword,
);
router.post("/logout", authenticate, authController.logout);
router.post("/refresh-token", authenticate, authController.refreshToken);

module.exports = router;
