const express = require('express');
const router = express.Router();
const AssetController = require('../controllers/asset.controller');
const { authenticateToken } = require('../middleware/auth');
const { canViewAssets, canModifyAssets } = require('../middleware/roleCheck');

// All asset routes require authentication
router.use(authenticateToken);

// Own assets – any authenticated user
router.get('/mine', AssetController.getMyAssets);

// Statistics (Super Admin, Admin, Bearbeiter can view)
router.get('/stats', canViewAssets, AssetController.getStatistics);

// View routes (Super Admin, Admin, Bearbeiter can view)
router.get('/', canViewAssets, AssetController.getAllAssets);
router.get('/status/:status', canViewAssets, AssetController.getAssetsByStatus);
router.get('/type/:type', canViewAssets, AssetController.getAssetsByType);
router.get('/serial/:serialNumber', canViewAssets, AssetController.getAssetBySerial);
router.get('/:id/label', canViewAssets, AssetController.generateLabel);
router.get('/:id/handover-protocol', canViewAssets, AssetController.generateHandoverProtocol);
router.get('/:id/history', canViewAssets, AssetController.getAssetAssignmentHistory);
router.get('/:id', canViewAssets, AssetController.getAssetById);

// Inspection routes
router.get('/:id/inspections', canViewAssets, AssetController.getInspections);
router.post('/:id/inspections', canModifyAssets, AssetController.createInspection);

// Modification routes (Super Admin, Admin only)
router.post('/:id/sync-agent', canModifyAssets, AssetController.syncFromAgent);
router.post('/import/intune', canModifyAssets, AssetController.importFromIntune);
router.post('/', canModifyAssets, AssetController.createAsset);
router.put('/:id', canModifyAssets, AssetController.updateAsset);
router.post('/:id/assign', canModifyAssets, AssetController.assignAsset);
router.post('/:id/unassign', canModifyAssets, AssetController.unassignAsset);
router.delete('/:id', canModifyAssets, AssetController.deleteAsset);

module.exports = router;
