const express = require('express');
const router = express.Router();
const { authenticateToken } = require('../middleware/auth');
const { requireAdmin } = require('../middleware/roleCheck');
const ctrl = require('../controllers/patch.controller');

// Agent-Callback — kein JWT, nur X-Agent-Key
router.post('/commands/result', ctrl.reportCommandResult);

router.use(authenticateToken);

router.get('/overview',              ctrl.getOverview);
router.get('/groups',                ctrl.getGroups);
router.post('/groups',               requireAdmin, ctrl.createGroup);
router.put('/groups/:id',            requireAdmin, ctrl.updateGroup);
router.delete('/groups/:id',         requireAdmin, ctrl.deleteGroup);

router.post('/policies',             requireAdmin, ctrl.upsertPolicy);
router.delete('/policies/:id',       requireAdmin, ctrl.deletePolicy);

router.post('/assign',               requireAdmin, ctrl.assignAgent);
router.post('/groups/:id/release',   requireAdmin, ctrl.releaseVersionToGroup);

router.get('/commands',              ctrl.getCommands);
router.post('/commands/trigger',     requireAdmin, ctrl.triggerCommand);
router.post('/commands/trigger-group', requireAdmin, ctrl.triggerGroupCommand);
// (result route is before authenticateToken above)

// Remote Shell
router.post('/shell/:agentId',              requireAdmin, ctrl.triggerShell);
router.get('/shell/:agentId/result/:cmdId', requireAdmin, ctrl.getShellResult);
router.get('/shell/:agentId/history',       requireAdmin, ctrl.getShellHistory);

module.exports = router;
