import { Router } from 'express';
import { body, param } from 'express-validator';
import { getAll, create, resume, remove } from '../controllers/heldBillController.js';
import { protect } from '../middlewares/auth.js';
import { handleValidation } from '../utils/handleValidation.js';

const router = Router();

const validateHeldBill = [
  body('items').isArray({ min: 1 }).withMessage('At least one item is required'),
  body('items.*.productId').isMongoId().withMessage('Invalid product id'),
  body('items.*.quantity').isInt({ min: 1, max: 9999 }).withMessage('Quantity must be 1-9999'),
  body('items.*.price').isFloat({ min: 0 }).withMessage('Price cannot be negative'),
  body('customerName')
    .optional({ nullable: true })
    .isString()
    .trim()
    .isLength({ max: 120 })
    .withMessage('Customer name cannot exceed 120 characters'),
  body('customerPhone')
    .optional({ nullable: true })
    .isString()
    .trim()
    .isLength({ max: 20 })
    .withMessage('Customer phone cannot exceed 20 characters'),
];

router.get('/', protect, getAll);
router.post('/', protect, validateHeldBill, handleValidation, create);
router.put('/:id/resume', protect, param('id').isMongoId(), handleValidation, resume);
router.delete('/:id', protect, param('id').isMongoId(), handleValidation, remove);

export default router;
