const express = require('express');
const router = express.Router();
const { getAlerts, createAlert, assignAlert, addNote, getLeaderboard } = require('../controllers/alertController');
const alertController = require('../controllers/alertController');
const { protect, authorize, requireTraining } = require('../middleware/authMiddleware');

router.get('/', protect, alertController.getAlerts);
router.get('/dashboard/stats', protect, alertController.getDashboardStats); // SOC Dashboard stats
router.get('/my', protect, alertController.getMyAlerts); // New route for user's investigations
router.get('/closed', protect, alertController.getClosedAlerts); // New route for closed alerts
router.get('/admin/investigations', protect, authorize('admin', 'alert-admin'), alertController.getInvestigationsForAdmin); // New route for admin review
router.get('/admin/assigned', protect, authorize('admin', 'alert-admin'), alertController.getAllAssignedAlerts); // New route for active assignments
router.post('/', protect, authorize('admin', 'alert-admin'), alertController.createAlert);

router.post('/generate', protect, authorize('admin', 'alert-admin'), alertController.generateAlerts);
router.post('/:id/assign', protect, requireTraining, alertController.assignAlert);
router.post('/:id/unassign', protect, alertController.unassignAlert);
router.post('/:id/note', protect, alertController.addNote);
router.post('/:id/submit', protect, requireTraining, alertController.submitSummary); // New route for submitting summary
router.post('/:id/complete', protect, requireTraining, alertController.completeInvestigation); // New route for Tier 1 playbook completion
router.post('/:id/complete-v2', protect, requireTraining, alertController.completeInvestigationV2); // V2 guided playbook completion
router.post('/investigation/:id/report', protect, alertController.reportInvestigation); // New route for user reporting
router.get('/admin/reported', protect, authorize('admin', 'alert-admin'), alertController.getReportedInvestigations); // Admin: fetch reported
router.post('/investigation/:id/grade', protect, authorize('admin', 'alert-admin'), alertController.gradeInvestigation); // New route for grading investigation
router.post('/investigation/:id/reset', protect, authorize('admin', 'alert-admin'), alertController.resetInvestigation); // New route for resetting investigation
router.post('/:id/reset-mine', protect, alertController.resetMyInvestigation); // Allow users to retry academy alerts
router.get('/archived', protect, authorize('admin', 'alert-admin'), alertController.getArchivedAlerts); // New route for archived alerts
router.post('/:id/resubmit', protect, authorize('admin', 'alert-admin'), alertController.resubmitAlert); // New route for resubmitting archived alert to main feed
router.put('/:id', protect, authorize('admin', 'alert-admin'), alertController.updateAlert);
// Content tagging routes
router.post('/bulk-tag', protect, authorize('admin', 'alert-admin'), alertController.bulkTagAlerts);
router.get('/path-stats', protect, authorize('admin', 'alert-admin'), alertController.getPathStats);
router.get('/sources', protect, alertController.getDistinctSources);
// Public stats route
router.get('/stats', alertController.getPublicStats);

// Careful with order: put /all before /:id to avoid collision if id is not regex validated (though usually it is fine if strict)
// But to be safe:
router.get('/leaderboard', alertController.getLeaderboard);
router.post('/leaderboard/reset', protect, authorize('admin', 'alert-admin'), alertController.resetLeaderboard);

router.get('/:id', protect, alertController.getAlertById);
router.delete('/all', protect, authorize('admin', 'alert-admin'), alertController.deleteAllAlerts);
router.delete('/:id', protect, authorize('admin', 'alert-admin'), alertController.deleteAlert);

module.exports = router;
