// routes/transactionRoutes.js
const express = require('express');
const router = express.Router();
const transactionController = require('../controllers/transactionController');
// const { protect, authorize } = require('../middleware/auth');

const { authenticate, authorize } = require('../middleware/auth');


// NEW ROUTE: Update transaction status (for M-Pesa callback)
router.put('/:transactionId/status', transactionController.updateTransactionStatus);


 router.get('/sales/recent', transactionController.getRecentTransactions);
 router.get('/sales/payment-stats', transactionController.getPaymentStatistics);

 router.post('/check-manager-pin', transactionController.checkManagerPin);

// All routes require authentication
router.use(authenticate);


// Get today's transactions for a specific user (GET /api/transactions/user/:userId/today)
// Query params: registerId, status, paymentMethod
router.get('/users/:userId/today',
  authenticate,
  transactionController.getUserTodayTransactions
);

// 7. Get all suspended transactions
router.get('/suspended', authenticate, authorize('admin', 'manager','cashier'),transactionController.getSuspendedTransactions);


// Create new transaction (sale)
// router.post('/', transactionController.createTransaction);

// Create new transaction (sale)
router.post('/', transactionController.createTransaction);

// Get all transactions with filters
router.get('/', transactionController.getAllTransactions);

//router.get('/dashboard-data', authenticate,transactionController.fetchDashboardData);



// Get sales summary/statistics
router.get('/summary', transactionController.getSalesSummary);

// Get daily sales report
router.get('/daily-report', transactionController.getDailySalesReport);

// Get transaction by ID
router.get('/:id', transactionController.getTransactionById);





// ==================== USER-SPECIFIC ROUTES ====================

// Get all transactions for a specific user (GET /api/transactions/user/:userId)
// Query params: registerId, date, startDate, endDate, status, paymentMethod, page, limit
// Example: /api/transactions/user/123?date=2024-01-15
// Example: /api/transactions/user/123?startDate=2024-01-01&endDate=2024-01-31
router.get('/user/:userId',
  authenticate,
  transactionController.getUserTransactions
);



// ==================== REGISTER-SPECIFIC ROUTES ====================

// Get all transactions for a specific register (GET /api/transactions/register/:registerId)
// Query params: date, startDate, endDate, status, cashierId, page, limit
router.get('/register/:registerId',
  authenticate,
  transactionController.getRegisterTransactions
);



// Get transaction by transaction number
router.get('/number/:transactionNumber', transactionController.getTransactionByNumber);




// Void/cancel transaction (admin/manager only)
router.post('/:id/void',authenticate, transactionController.voidTransaction);



router.post('/:id/reverse',  authorize('admin', 'manager'),transactionController.reverseTransaction);
//router.post('/suspend', authorize('admin', 'cashier'), transactionController.suspendTransaction);
router.get('/:id/resume', authorize('admin', 'manager','cashier'), transactionController.resumeSuspendedTransaction);
//router.get('/transactions/suspended',authorize('admin', 'manager','cashier'), transactionController.getSuspendedTransactions);
router.delete('/transactions/suspended/:id',authorize('admin','cashier'), transactionController.deleteSuspendedTransaction);
router.post('/:transactionId/exchange',authorize('admin','cashier'), transactionController.exchangeItem);
router.get('/reprint/:transactionNumber',authorize('admin','cashier'), transactionController.getTransactionForReprint);



// ============================================
// TRANSACTION LIFECYCLE ROUTES
// ============================================

// 1. Start new transaction (OPEN status)
router.post('/start', authenticate, transactionController.startTransaction);

// 2. Add items to open transaction
router.post('/:transactionId/add-items', authenticate, transactionController.addItemsToTransaction);

// 3. Remove items from transaction
router.delete('/:transactionId/items/:itemId', authenticate, transactionController.removeItemFromTransaction);

// 4. Update item quantity
router.patch('/:transactionId/items/:itemId', authenticate, transactionController.updateItemQuantity);

// 5. Suspend transaction (SUSPENDED status)
router.post('/:transactionId/suspend', authenticate, transactionController.suspendTransaction);

// 6. Resume suspended transaction (back to OPEN)
router.post('/:transactionId/resume', authenticate, transactionController.resumeTransaction);



// 8. Delete suspended transaction
router.delete('/:transactionId/suspended', authenticate, transactionController.deleteSuspendedTransaction);

// 9. Finalize transaction (PENDING_PAYMENT status)
router.post('/:transactionId/finalize', authenticate, transactionController.finalizeTransaction);

// 10. Process payment (PAID/AUTHORIZED status)
router.post('/:transactionId/process-payment', authenticate, transactionController.processPayment);

// 11. Complete transaction (COMPLETED status)
router.post('/:transactionId/complete', authenticate, transactionController.completeTransaction);


module.exports = router;

