// middleware/validation.js
const { body, param, query, validationResult } = require('express-validator');

exports.validate = (req, res, next) => {
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(400).json({ 
      message: 'Validation error', 
      errors: errors.array() 
    });
  }
  next();
};

// Validation rules
exports.userValidation = [
  body('email').isEmail().withMessage('Invalid email'),
  body('password').isLength({ min: 6 }).withMessage('Password must be at least 6 characters'),
  body('firstName').notEmpty().withMessage('First name is required'),
  body('lastName').notEmpty().withMessage('Last name is required'),
];

exports.productValidation = [
  body('name').notEmpty().withMessage('Product name is required'),
  body('sku').notEmpty().withMessage('SKU is required'),
  body('unitPrice').isFloat({ min: 0 }).withMessage('Unit price must be a positive number'),
  body('categoryId').isInt().withMessage('Category ID must be an integer'),
];

exports.transactionValidation = [
  body('storeId').isInt().withMessage('Store ID is required'),
  body('registerId').isInt().withMessage('Register ID is required'),
  body('items').isArray({ min: 1 }).withMessage('At least one item is required'),
];

module.exports = exports;