import { Router } from 'express';
import { mobileController } from './mobile.controller.js';
import { authenticate } from '../../shared/middleware/auth.middleware.js';
import { requireAccess } from '../../shared/middleware/rbac.middleware.js';

const router = Router();

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

// ============================================================================
// SESSION ROUTES
// ============================================================================

// Create a new mobile session
router.post('/sessions', requireAccess({ roles: ['admin', 'manager', 'sales', 'cashier'], permission: 'mobile:create' }), (req, res, next) =>
  mobileController.createSession(req, res, next)
);

// List active sessions
router.get('/sessions', requireAccess({ roles: ['admin', 'manager'], permission: 'mobile:read' }), (req, res, next) =>
  mobileController.listSessions(req, res, next)
);

// Revoke a mobile session
router.delete('/sessions/:id', requireAccess({ roles: ['admin', 'manager'], permission: 'mobile:delete' }), (req, res, next) =>
  mobileController.revokeSession(req, res, next)
);

// ============================================================================
// PUSH TOKEN ROUTES
// ============================================================================

// Register a push token
router.post('/push-tokens', requireAccess({ roles: ['admin', 'manager', 'sales', 'cashier'], permission: 'mobile:create' }), (req, res, next) =>
  mobileController.registerPushToken(req, res, next)
);

// Remove a push token
router.delete('/push-tokens/:id', requireAccess({ roles: ['admin', 'manager', 'sales', 'cashier'], permission: 'mobile:delete' }), (req, res, next) =>
  mobileController.removePushToken(req, res, next)
);

// ============================================================================
// SYNC QUEUE ROUTES
// ============================================================================

// Get sync queue items
router.get('/sync-queue', requireAccess({ roles: ['admin', 'manager', 'sales', 'cashier'], permission: 'mobile:read' }), (req, res, next) =>
  mobileController.getQueueItems(req, res, next)
);

// Add item to sync queue
router.post('/sync-queue', requireAccess({ roles: ['admin', 'manager', 'sales', 'cashier'], permission: 'mobile:create' }), (req, res, next) =>
  mobileController.addToQueue(req, res, next)
);

// Process a sync queue item
router.put('/sync-queue/:id/process', requireAccess({ roles: ['admin', 'manager'], permission: 'mobile:update' }), (req, res, next) =>
  mobileController.processQueueItem(req, res, next)
);

export default router;
