const express = require('express');
const router = express.Router();
const ProductController = require('../controllers/product');
const { check } = require('express-validator');
const { authenticate, optionalAuth } = require('../middleware/auth');
const upload = require('../config/multer');

// Create product
router.post(
  '/',
  authenticate,
  upload.single('file'),
  ProductController.createProduct
);

router.get('/top', ProductController.getTopProducts);

// Get product by ID
router.get('/:id', optionalAuth, ProductController.getProduct);

// Update product
router.patch(
  '/:id',
  authenticate,
  //   check('price', 'Price must be a positive number')
  //     .optional()
  //     .isFloat({ min: 0 }),
  //   check('stock', 'Stock must be a non-negative integer')
  //     .optional()
  //     .isInt({ min: 0 }),
  // ],
  ProductController.updateProduct
);

// Delete product
router.delete('/:id', authenticate, ProductController.deleteProduct);

// Get products by category
router.get(
  '/category/:categoryName',
  optionalAuth,
  ProductController.getProductsByCategory
);

// List products
router.get('/', optionalAuth, ProductController.listProducts);

router.get('/user/:userId', optionalAuth, ProductController.getProductsByUser);

router.get(
  '/user-dashboard/:userId',
  ProductController.getUserDashboardProducts
);

// Toggle product status
router.patch(
  '/:id/toggle-status',
  authenticate,
  ProductController.toggleProductStatus
);

module.exports = router;
