// Operations Routes - Narrative Campaigns
const express = require('express');
const router = express.Router();
const { protect, optionalAuth } = require('../middleware/authMiddleware');
const {
    listOperations,
    listPublicOperations,
    getPublicOperationDetails,
    getGuestOperationAlert,
    gradeGuestInvestigation,
    getOperationDetails,
    startOperation,
    assignDailyMission,
    getIntelReports,
    getCompletionReport
} = require('../controllers/operationController');

// List public operations (for landing page)
router.get('/public', listPublicOperations);

// Get public operation details (for SEO pages - no auth)
router.get('/public/:id', getPublicOperationDetails);

// Guest investigation routes (first 2 alerts of free ops)
router.get('/public/:id/alerts/:seq', optionalAuth, getGuestOperationAlert);
router.post('/public/:id/alerts/:seq/submit', gradeGuestInvestigation);

// List all operations (with user progress)
router.get('/', protect, listOperations);

// Get operation details
router.get('/:id', protect, getOperationDetails);

// Start an operation
router.post('/:id/start', protect, startOperation);

// Get unlocked intel reports
router.get('/:id/intel', protect, getIntelReports);

// Get completion report for finished operation
router.get('/:id/completion-report', protect, getCompletionReport);

// Assign daily mission
router.post('/assign-daily', protect, assignDailyMission);

/* --- Admin Routes --- */
const { admin } = require('../middleware/authMiddleware');
const {
    createOperation,
    updateOperation,
    deleteOperation,
    generateOperationWithAI,
    listOperationsAdmin,
    addAlertToOperation,
    removeAlertFromOperation,
    reorderAlerts,
    getOperationStats,
    getOperationForEdit
} = require('../controllers/operationController');

// Admin: List all operations with stats
router.get('/admin/list', protect, admin, listOperationsAdmin);

// Admin: Get operation for editing
router.get('/admin/:id', protect, admin, getOperationForEdit);

// Admin: Get operation statistics
router.get('/admin/:id/stats', protect, admin, getOperationStats);

// Generate operation with AI
router.post('/generate', protect, admin, generateOperationWithAI);

// Create new operation (manual)
router.post('/', protect, admin, createOperation);

// Update operation
router.put('/:id', protect, admin, updateOperation);

// Delete operation
router.delete('/:id', protect, admin, deleteOperation);

// Admin: Add alert to operation
router.post('/:id/alerts', protect, admin, addAlertToOperation);

// Admin: Remove alert from operation
router.delete('/:id/alerts/:alertId', protect, admin, removeAlertFromOperation);

// Admin: Reorder alerts
router.put('/:id/alerts/reorder', protect, admin, reorderAlerts);

module.exports = router;
