// routes/customerRoutes.js
'use strict';

const express = require('express');
const router = express.Router();
const customerController = require('../controllers/customerController');

// Import middleware (create these if they don't exist)
const { authenticate, authorize } = require('../middleware/auth');
const { validateCustomer, validateAddress, validatePayment } = require('../middleware/validation');
const { rateLimiter } = require('../middleware/rateLimiter');

// ============================================
// 📊 DASHBOARD & ANALYTICS ROUTES
// ============================================

/**
 * @route   GET /api/customers/:id/dashboard
 * @desc    Get customer dashboard with statistics
 * @access  Private (Customer/Admin)
 */
router.get(
  '/:id/dashboard',
  authenticate,
  customerController.getCustomerDashboard
);

/**
 * @route   GET /api/customers/:id/insights
 * @desc    Get customer shopping insights and patterns
 * @access  Private (Customer/Admin)
 */
router.get(
  '/:id/insights',
  authenticate,
  customerController.getCustomerInsights
);

/**
 * @route   GET /api/customers/:id/activity-report
 * @desc    Get customer activity report for a date range
 * @access  Private (Customer/Admin)
 */
router.get(
  '/:id/activity-report',
  authenticate,
  customerController.getCustomerActivityReport
);

/**
 * @route   GET /api/customers/statistics
 * @desc    Get overall customer statistics (Admin only)
 * @access  Private (Admin)
 */
router.get(
  '/statistics',
  authenticate,
//   authorize(['admin', 'manager']),
  customerController.getCustomerStatistics
);


// In customerRoutes.js
router.get(
  '/by-user/:userId',
  authenticate,
  customerController.getCustomerByUserId
);

// ============================================
// 👤 CUSTOMER MANAGEMENT ROUTES
// ============================================

/**
 * @route   GET /api/customers
 * @desc    Get all customers with filtering and pagination
 * @access  Private (Admin/Manager)
 */
router.get(
  '/',
  authenticate,
//   authorize(['admin', 'manager']),
  customerController.getAllCustomers
);

/**
 * @route   GET /api/customers/search
 * @desc    Search customers by name, email, or phone
 * @access  Private (Admin/Manager)
 */
router.get(
  '/search',
  authenticate,
//   authorize(['admin', 'manager']),
  customerController.searchCustomers
);

/**
 * @route   GET /api/customers/:id
 * @desc    Get customer by ID with full details
 * @access  Private (Customer/Admin)
 */
router.get(
  '/:id',
  authenticate,
  customerController.getCustomerById
);

/**
 * @route   POST /api/customers
 * @desc    Create new customer
 * @access  Public (with rate limiting)
 */
router.post(
  '/',
  rateLimiter(5, 15), // 5 requests per 15 minutes
  validateCustomer,
  customerController.createCustomer
);

/**
 * @route   PUT /api/customers/:id
 * @desc    Update customer profile
 * @access  Private (Customer/Admin)
 */
router.put(
  '/:id',
  authenticate,
  validateCustomer,
  customerController.updateCustomer
);

/**
 * @route   PUT /api/customers/:id/password
 * @desc    Update customer password
 * @access  Private (Customer)
 */
router.put(
  '/:id/password',
  authenticate,
  customerController.updatePassword
);

/**
 * @route   DELETE /api/customers/:id
 * @desc    Delete or deactivate customer
 * @access  Private (Admin)
 */
router.delete(
  '/:id',
  authenticate,
  authorize(['admin']),
  customerController.deleteCustomer
);

/**
 * @route   GET /api/customers/:id/export
 * @desc    Export customer data (GDPR compliance)
 * @access  Private (Customer/Admin)
 */
router.get(
  '/:id/export',
  authenticate,
  rateLimiter(3, 60), // 3 exports per hour
  customerController.exportCustomerData
);

// ============================================
// 📍 ADDRESS MANAGEMENT ROUTES
// ============================================

/**
 * @route   GET /api/customers/:id/addresses
 * @desc    Get all addresses for a customer
 * @access  Private (Customer/Admin)
 */
router.get(
  '/:id/addresses',
  authenticate,
  customerController.getCustomerAddresses
);

/**
 * @route   POST /api/customers/:id/addresses
 * @desc    Add new address for customer
 * @access  Private (Customer/Admin)
 */
router.post(
  '/:id/addresses',
  authenticate,
  validateAddress,
  customerController.addCustomerAddress
);

/**
 * @route   PUT /api/customers/:id/addresses/:addressId
 * @desc    Update customer address
 * @access  Private (Customer/Admin)
 */
router.put(
  '/:id/addresses/:addressId',
  authenticate,
  validateAddress,
  customerController.updateCustomerAddress
);

/**
 * @route   DELETE /api/customers/:id/addresses/:addressId
 * @desc    Delete customer address
 * @access  Private (Customer/Admin)
 */
router.delete(
  '/:id/addresses/:addressId',
  authenticate,
  customerController.deleteCustomerAddress
);

// ============================================
// 💳 TRANSACTION & LOYALTY ROUTES
// ============================================

/**
 * @route   GET /api/customers/:id/transactions
 * @desc    Get all transactions for a customer
 * @access  Private (Customer/Admin)
 */
router.get(
  '/:id/transactions',
  authenticate,
  customerController.getCustomerTransactions
);

/**
 * @route   PUT /api/customers/:id/loyalty-points
 * @desc    Update customer loyalty points
 * @access  Private (Admin)
 */
router.put(
  '/:id/loyalty-points',
  authenticate,
  authorize(['admin', 'manager']),
  customerController.updateLoyaltyPoints
);

/**
 * @route   POST /api/customers/:id/redeem-points
 * @desc    Redeem loyalty points
 * @access  Private (Customer)
 */
router.post(
  '/:id/redeem-points',
  authenticate,
  rateLimiter(10, 60), // 10 redemptions per hour
  customerController.redeemLoyaltyPoints
);

/**
 * @route   GET /api/customers/:id/tier-info
 * @desc    Get customer tier information and progress
 * @access  Private (Customer/Admin)
 */
router.get(
  '/:id/tier-info',
  authenticate,
  customerController.getTierInfo
);

// ============================================
// 💰 PAYMENT ROUTES
// ============================================

/**
 * @route   GET /api/customers/:id/payment-methods
 * @desc    Get customer's payment methods
 * @access  Private (Customer/Admin)
 */
router.get(
  '/:id/payment-methods',
  authenticate,
  customerController.getPaymentMethods
);

/**
 * @route   GET /api/customers/:id/payments
 * @desc    Get customer payment history
 * @access  Private (Customer/Admin)
 */
router.get(
  '/:id/payments',
  authenticate,
  customerController.getCustomerPayments
);

/**
 * @route   POST /api/customers/:id/payments
 * @desc    Process a new payment
 * @access  Private (Customer/Admin)
 */
router.post(
  '/:id/payments',
  authenticate,
  validatePayment,
  rateLimiter(20, 60), // 20 payments per hour
  customerController.processPayment
);

/**
 * @route   GET /api/customers/:id/payments/:paymentId/verify
 * @desc    Verify payment status
 * @access  Private (Customer/Admin)
 */
router.get(
  '/:id/payments/:paymentId/verify',
  authenticate,
  customerController.verifyPayment
);

/**
 * @route   GET /api/customers/:id/payment-stats
 * @desc    Get payment statistics for customer
 * @access  Private (Customer/Admin)
 */
router.get(
  '/:id/payment-stats',
  authenticate,
  customerController.getPaymentStatistics
);

/**
 * @route   POST /api/customers/:id/payments/:paymentId/refund
 * @desc    Process payment refund
 * @access  Private (Admin)
 */
router.post(
  '/:id/payments/:paymentId/refund',
  authenticate,
  authorize(['admin', 'manager']),
  customerController.refundPayment
);

// ============================================
// ⚙️ PREFERENCES & SETTINGS ROUTES
// ============================================

/**
 * @route   PUT /api/customers/:id/preferences
 * @desc    Update customer preferences
 * @access  Private (Customer)
 */
router.put(
  '/:id/preferences',
  authenticate,
  customerController.updatePreferences
);

// ============================================
// 📦 ORDER MANAGEMENT ROUTES
// ============================================

/**
 * @route   GET /api/customers/:id/orders
 * @desc    Get all orders for a customer with filtering
 * @access  Private (Customer/Admin)
 */
router.get(
  '/:id/orders',
  authenticate,
  customerController.getCustomerOrders
);

/**
 * @route   GET /api/customers/:id/orders/active
 * @desc    Get active orders (pending, confirmed, processing, shipped)
 * @access  Private (Customer/Admin)
 */
router.get(
  '/:id/orders/active',
  authenticate,
  customerController.getActiveOrders
);

/**
 * @route   GET /api/customers/:id/orders/history
 * @desc    Get order history (completed/delivered orders)
 * @access  Private (Customer/Admin)
 */
router.get(
  '/:id/orders/history',
  authenticate,
  customerController.getOrderHistory
);

/**
 * @route   GET /api/customers/:id/orders/recent
 * @desc    Get recent orders (last 10)
 * @access  Private (Customer/Admin)
 */
router.get(
  '/recent/:id/orders',
  authenticate,
  customerController.getRecentOrders
);

/**
 * @route   GET /api/customers/:id/orders/statistics
 * @desc    Get order statistics for customer
 * @access  Private (Customer/Admin)
 */
router.get(
  '/:id/orders/statistics',
  authenticate,
  customerController.getOrderStatistics
);

/**
 * @route   GET /api/customers/:id/orders/:orderId
 * @desc    Get specific order details
 * @access  Private (Customer/Admin)
 */
router.get(
  '/:id/orders/:orderId',
  authenticate,
  customerController.getCustomerOrderById
);

/**
 * @route   GET /api/customers/:id/orders/:orderId/track
 * @desc    Track order shipment
 * @access  Private (Customer/Admin)
 */
router.get(
  '/:id/orders/:orderId/track',
  authenticate,
  customerController.trackOrder
);

/**
 * @route   PUT /api/customers/:id/orders/:orderId/cancel
 * @desc    Cancel an order
 * @access  Private (Customer/Admin)
 */
router.put(
  '/:id/orders/:orderId/cancel',
  authenticate,
  rateLimiter(10, 60), // 10 cancellations per hour
  customerController.cancelOrder
);

/**
 * @route   POST /api/customers/:id/orders/:orderId/reorder
 * @desc    Reorder items from a previous order
 * @access  Private (Customer)
 */
router.post(
  '/:id/orders/:orderId/reorder',
  authenticate,
  customerController.reorderItems
);

module.exports = router;