import { body, param } from 'express-validator';

const RETURN_REASONS = [
  'CUSTOMER_RETURN',
  'DAMAGED_PRODUCT',
  'WRONG_PRODUCT',
  'DEFECTIVE_PRODUCT',
  'EXPIRED_PRODUCT',
  'OTHER',
];
const REFUND_METHODS = ['CASH', 'UPI', 'CARD', 'STORE_CREDIT'];

export const validateSaleId = [param('id').isMongoId().withMessage('Invalid sale id')];

export const validateReturn = [
  param('id').isMongoId().withMessage('Invalid sale id'),
  body('items')
    .isArray({ min: 1 })
    .withMessage('At least one return item is required'),
  body('items.*.product').isMongoId().withMessage('Invalid product id'),
  body('items.*.variantId')
    .optional({ nullable: true })
    .isString()
    .trim()
    .isLength({ max: 64 })
    .withMessage('Invalid variant id'),
  body('items.*.quantity')
    .isInt({ min: 1, max: 9999 })
    .withMessage('Return quantity must be between 1 and 9999'),
  body('reason')
    .isIn(RETURN_REASONS)
    .withMessage(`Reason must be one of: ${RETURN_REASONS.join(', ')}`),
  body('refundMethod')
    .optional()
    .isIn(REFUND_METHODS)
    .withMessage(`Refund method must be one of: ${REFUND_METHODS.join(', ')}`),
  body('refundReference')
    .optional({ nullable: true })
    .isString()
    .trim()
    .isLength({ max: 60 })
    .withMessage('Refund reference cannot exceed 60 characters'),
  body('notes')
    .optional({ nullable: true })
    .isString()
    .trim()
    .isLength({ max: 500 })
    .withMessage('Notes cannot exceed 500 characters'),
];
