const express = require('express');
const router = express.Router();

const shoppingController = require('../controllers/shoppingController');

// ============================================================================
// CART ROUTES
// ============================================================================

// Get or create cart (user or guest)
router.get('/cart', shoppingController.getCart);

// Add item to cart
router.post('/cart/add', shoppingController.addToCart);

// Update cart item quantity
router.put('/cart/item/:itemId', shoppingController.updateCartItem);

// Remove item from cart
router.delete('/cart/item/:itemId', shoppingController.removeFromCart);

// Clear entire cart
router.delete('/cart/:cartId', shoppingController.clearCart);

// ============================================================================
// CHECKOUT & ORDER CREATION
// ============================================================================

// Create order from cart
router.post('/checkout', shoppingController.createOrder);

// Update order payment status (manual or internal)
router.put(
  '/orders/:orderId/payment-status',
  shoppingController.updateOrderPaymentStatus
);

// ============================================================================
// ORDER MANAGEMENT (CUSTOMER)
// ============================================================================

// Get single order by public orderId
router.get('/orders/:orderId', shoppingController.getOrder);

// Get orders for a customer
router.get(
  '/orders/customer/:customerId',
  shoppingController.getCustomerOrders
);

// Cancel order
router.put('/orders/:orderId/cancel', shoppingController.cancelOrder);

// ============================================================================
// SHIPMENT TRACKING
// ============================================================================

// Update shipment status (admin / logistics)
router.put(
  '/shipments/:trackingNumber',
  shoppingController.updateShipment
);

// Track shipment (public)
router.get(
  '/shipments/track/:trackingNumber',
  shoppingController.trackShipment
);

// ============================================================================
// ADMIN ROUTES
// ============================================================================

// Get all orders
router.get('/admin/orders', shoppingController.getAllOrders);

// Update order status
router.put(
  '/admin/orders/:orderId/status',
  shoppingController.updateOrderStatus
);

// Get order statistics
router.get(
  '/admin/statistics',
  shoppingController.getOrderStatistics
);

// Abandoned carts
router.get(
  '/admin/abandoned-carts',
  shoppingController.getAbandonedCarts
);

// Cleanup expired carts (cron / manual)
router.delete(
  '/admin/cleanup-carts',
  shoppingController.cleanupExpiredCarts
);

// ============================================================================
// WEBHOOKS
// ============================================================================

// M-Pesa callback
router.post(
  '/webhooks/mpesa-callback',
  shoppingController.handleMpesaCallback
);

module.exports = router;
