// routes/registerRoutes.js
const express = require('express');
const router = express.Router();
const registerController = require('../controllers/registerController');
const CashDrawerController = require('../controllers/cashDrawerController');
const { authenticate, authorize } = require('../middleware/auth');

// ============================================
// REGISTER ROUTES
// ============================================

router.use(authenticate);


// ALWAYS USE THE MORE SPECIFIC ROUTES FIRST TO AVOID CONFLICTS

// Get registers assigned to a specific user
// GET /api/registers/user/:id/assignments

router.get('/:userId/assignments', authenticate, registerController.getUserAssignedRegisters);


router.get('/:id/cashwith',authenticate,CashDrawerController.getCurrentSession);


router.get('/all', authenticate,authorize('admin', 'manager'), registerController.getAllRegisters);


// Get all registers for a specific store
// GET /api/stores/:storeId/registers
router.get('/:storeId/registers', authenticate, registerController.getStoreRegisters);

router.get('/today-sales-summary', registerController.getTodaySalesSummary);

router.post('/:id/assign', authenticate, authorize('admin', 'manager'), registerController.assignUserToRegister);


router.get('/:id/assigned-users', authenticate, authorize('admin', 'manager'), registerController.getRegisterAssignedUsers);


router.get('/registers/sales-summary',authorize('admin', 'manager'), registerController.getRegisterSalesSummary);







// Get single register by ID
// GET /api/registers/:id
router.get('/:id', authenticate, registerController.getRegisterById);

// Get register statistics
// GET /api/registers/:id/stats
router.get('/:id/stats', authenticate, registerController.getRegisterStats);




// Create new register
// POST /api/registers
// Body: { name, storeId, openingCash, notes }
router.post('/', authenticate, authorize('admin', 'manager'), registerController.createRegister);

// Update register
// PUT /api/registers/:id
// Body: { name, openingCash, notes }
router.put('/:id', authenticate, authorize('admin', 'manager'), registerController.updateRegister);

// Open register (start shift)
// POST /api/registers/:id/open
// Body: { openingCash, openedBy }
router.post('/:id/open', authenticate, authorize('admin', 'manager', 'cashier'), registerController.openRegister);

// Close register (end shift)
// POST /api/registers/:id/close
// Body: { closingCash, notes, closedBy }
router.post('/:id/close', authenticate, authorize('admin', 'manager', 'cashier'), registerController.closeRegister);

// Update register cash (for transactions)
// PUT /api/registers/:id/cash
// Body: { amount, type: 'add' | 'subtract', transactionId }
router.put('/:id/cash', authenticate, registerController.updateRegisterCash);

// Delete register
// DELETE /api/registers/:id
router.delete('/:id', authenticate, authorize('admin', 'manager'), registerController.deleteRegister);




module.exports = router;