'use strict';

const { body, param, query } = require('express-validator');

/**
 * Validation for creating a return
 */
const validateReturn = [
  body('transactionId')
    .notEmpty()
    .withMessage('Transaction ID is required')
    .isInt()
    .withMessage('Transaction ID must be an integer'),
  
  body('storeId')
    .notEmpty()
    .withMessage('Store ID is required')
    .isInt()
    .withMessage('Store ID must be an integer'),
  
  body('registerId')
    .notEmpty()
    .withMessage('Register ID is required')
    .isInt()
    .withMessage('Register ID must be an integer'),
  
  body('reason')
    .notEmpty()
    .withMessage('Return reason is required')
    .isLength({ min: 10, max: 500 })
    .withMessage('Reason must be between 10 and 500 characters'),
  
  body('items')
    .notEmpty()
    .withMessage('Return items are required')
    .isArray({ min: 1 })
    .withMessage('At least one item must be returned'),
  
  body('items.*.transactionItemId')
    .notEmpty()
    .withMessage('Transaction item ID is required')
    .isInt()
    .withMessage('Transaction item ID must be an integer'),
  
  body('items.*.quantity')
    .notEmpty()
    .withMessage('Quantity is required')
    .isInt({ min: 1 })
    .withMessage('Quantity must be at least 1'),
  
  body('items.*.reason')
    .optional()
    .isLength({ max: 500 })
    .withMessage('Item reason must not exceed 500 characters'),
  
  body('items.*.condition')
    .optional()
    .isIn(['new', 'good', 'damaged', 'defective'])
    .withMessage('Invalid condition value'),
  
  body('customerId')
    .optional()
    .isInt()
    .withMessage('Customer ID must be an integer'),
  
  body('refundMethod')
    .optional()
    .isIn(['original_payment', 'cash', 'store_credit', 'exchange'])
    .withMessage('Invalid refund method'),
  
  body('notes')
    .optional()
    .isLength({ max: 1000 })
    .withMessage('Notes must not exceed 1000 characters')
];

/**
 * Validation for approving a return
 */
const validateReturnApproval = [
  param('id')
    .notEmpty()
    .withMessage('Return ID is required')
    .isInt()
    .withMessage('Return ID must be an integer'),
  
  body('restockItems')
    .optional()
    .isBoolean()
    .withMessage('Restock items must be a boolean'),
  
  body('notes')
    .optional()
    .isLength({ max: 1000 })
    .withMessage('Notes must not exceed 1000 characters')
];

/**
 * Validation for rejecting a return
 */
const validateReturnRejection = [
  param('id')
    .notEmpty()
    .withMessage('Return ID is required')
    .isInt()
    .withMessage('Return ID must be an integer'),
  
  body('rejectionReason')
    .notEmpty()
    .withMessage('Rejection reason is required')
    .isLength({ min: 10, max: 500 })
    .withMessage('Rejection reason must be between 10 and 500 characters')
];

/**
 * Validation for completing a return
 */
const validateReturnCompletion = [
  param('id')
    .notEmpty()
    .withMessage('Return ID is required')
    .isInt()
    .withMessage('Return ID must be an integer'),
  
  body('refundMethod')
    .optional()
    .isIn(['original_payment', 'cash', 'store_credit', 'exchange'])
    .withMessage('Invalid refund method'),
  
  body('refundAmount')
    .optional()
    .isDecimal({ decimal_digits: '0,2' })
    .withMessage('Refund amount must be a valid decimal'),
  
  body('paymentReference')
    .optional()
    .isLength({ max: 100 })
    .withMessage('Payment reference must not exceed 100 characters')
];

/**
 * Validation for updating a return
 */
const validateReturnUpdate = [
  param('id')
    .notEmpty()
    .withMessage('Return ID is required')
    .isInt()
    .withMessage('Return ID must be an integer'),
  
  body('reason')
    .optional()
    .isLength({ min: 10, max: 500 })
    .withMessage('Reason must be between 10 and 500 characters'),
  
  body('notes')
    .optional()
    .isLength({ max: 1000 })
    .withMessage('Notes must not exceed 1000 characters'),
  
  body('refundMethod')
    .optional()
    .isIn(['original_payment', 'cash', 'store_credit', 'exchange'])
    .withMessage('Invalid refund method'),
  
  body('restockingFee')
    .optional()
    .isDecimal({ decimal_digits: '0,2' })
    .withMessage('Restocking fee must be a valid decimal')
];

/**
 * Validation for batch operations
 */
const validateBatchApprove = [
  body('returnIds')
    .notEmpty()
    .withMessage('Return IDs are required')
    .isArray({ min: 1 })
    .withMessage('At least one return ID must be provided'),
  
  body('returnIds.*')
    .isInt()
    .withMessage('Each return ID must be an integer'),
  
  body('restockItems')
    .optional()
    .isBoolean()
    .withMessage('Restock items must be a boolean')
];

/**
 * Validation for query parameters
 */
const validateReturnQuery = [
  query('page')
    .optional()
    .isInt({ min: 1 })
    .withMessage('Page must be a positive integer'),
  
  query('limit')
    .optional()
    .isInt({ min: 1, max: 100 })
    .withMessage('Limit must be between 1 and 100'),
  
  query('status')
    .optional()
    .isIn(['pending', 'approved', 'rejected', 'completed', 'cancelled'])
    .withMessage('Invalid status value'),
  
  query('storeId')
    .optional()
    .isInt()
    .withMessage('Store ID must be an integer'),
  
  query('customerId')
    .optional()
    .isInt()
    .withMessage('Customer ID must be an integer'),
  
  query('startDate')
    .optional()
    .isISO8601()
    .withMessage('Start date must be a valid ISO 8601 date'),
  
  query('endDate')
    .optional()
    .isISO8601()
    .withMessage('End date must be a valid ISO 8601 date')
];

module.exports = {
  validateReturn,
  validateReturnApproval,
  validateReturnRejection,
  validateReturnCompletion,
  validateReturnUpdate,
  validateBatchApprove,
  validateReturnQuery
};