import { body } from 'express-validator';

export const createSaleValidator = [
  body('items')
    .isArray({ min: 1 })
    .withMessage('At least one item is required'),
  body('items.*.product').isMongoId().withMessage('Valid product ID is required'),
  body('items.*.quantity').isInt({ min: 1 }).withMessage('Quantity must be at least 1'),
  body('items.*.size').optional().trim(),
  body('items.*.variant').optional().isString().withMessage('Variant must be a string'),
  body('paymentMethod')
    .optional()
    .isIn(['CASH', 'UPI', 'CARD'])
    .withMessage('Payment method must be CASH, UPI or CARD'),
  body('customerName')
    .optional({ values: 'falsy' })
    .trim()
    .isLength({ max: 120 })
    .withMessage('Customer name cannot exceed 120 characters'),
  body('customerPhone')
    .optional({ values: 'falsy' })
    .trim()
    .matches(/^[0-9+\-\s]{0,20}$/)
    .withMessage('Customer phone is invalid'),
  body('amountReceived')
    .optional({ values: 'null' })
    .isFloat({ min: 0 })
    .withMessage('Amount received cannot be negative'),
];
