import { Router } from 'express';
import { tenantsController } from './tenants.controller.js';
import { authenticate, requireRoles } from '../../shared/middleware/auth.middleware.js';

const router = Router();

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

// Get current user's tenant (any authenticated user)
router.get('/current', (req, res, next) =>
  tenantsController.getCurrent(req, res, next)
);

// List all tenants (super_admin only)
router.get('/', requireRoles('super_admin'), (req, res, next) =>
  tenantsController.findAll(req, res, next)
);

// Get tenant by ID (super_admin only)
router.get('/:id', requireRoles('super_admin'), (req, res, next) =>
  tenantsController.findById(req, res, next)
);

// Get tenant statistics (super_admin only)
router.get('/:id/stats', requireRoles('super_admin'), (req, res, next) =>
  tenantsController.getStats(req, res, next)
);

// Create tenant (super_admin only)
router.post('/', requireRoles('super_admin'), (req, res, next) =>
  tenantsController.create(req, res, next)
);

// Update tenant (super_admin only)
router.put('/:id', requireRoles('super_admin'), (req, res, next) =>
  tenantsController.update(req, res, next)
);

// Suspend tenant (super_admin only)
router.post('/:id/suspend', requireRoles('super_admin'), (req, res, next) =>
  tenantsController.suspend(req, res, next)
);

// Activate tenant (super_admin only)
router.post('/:id/activate', requireRoles('super_admin'), (req, res, next) =>
  tenantsController.activate(req, res, next)
);

// Delete tenant (super_admin only)
router.delete('/:id', requireRoles('super_admin'), (req, res, next) =>
  tenantsController.delete(req, res, next)
);

// Tenant settings (admin and super_admin)
router.get('/:id/settings', requireRoles('admin', 'super_admin'), (req, res, next) =>
  tenantsController.getSettings(req, res, next)
);

router.put('/:id/settings', requireRoles('admin', 'super_admin'), (req, res, next) =>
  tenantsController.updateSettings(req, res, next)
);

// Check user limit (admin and super_admin)
router.get('/:id/can-add-user', requireRoles('admin', 'super_admin'), (req, res, next) =>
  tenantsController.canAddUser(req, res, next)
);

export default router;
