const express = require('express');
const router = express.Router();
const adminController = require('../controllers/adminController');
const { protect, authorize } = require('../middleware/authMiddleware');

const adminSettingsController = require('../controllers/adminSettingsController');
const newsletterController = require('../controllers/newsletterController');

router.get('/stats', protect, authorize('admin', 'alert-admin'), adminController.getStats);
router.get('/pulse', protect, authorize('admin', 'alert-admin'), adminController.getSystemPulse); // Phase 2: Pulse
// router.get('/pulse', adminController.getSystemPulse); // TEMP DEBUG
router.get('/active-users', protect, authorize('admin', 'alert-admin'), adminController.getActiveUsersDetails); // Phase 2: Pulse Details
router.get('/giveaway', protect, authorize('admin', 'alert-admin'), adminController.getGiveawayStatus);
router.post('/giveaway/award', protect, authorize('admin', 'alert-admin'), adminController.awardGiveaway);
router.post('/giveaway/award-top-3', protect, authorize('admin', 'alert-admin'), adminController.awardTop3);
router.post('/users/:id/unlock', protect, authorize('admin'), adminController.unlockUser);
router.put('/users/:id/verify', protect, authorize('admin'), adminController.verifyUser);
router.put('/users/:id/username', protect, authorize('admin'), adminController.updateUserUsername);
router.post('/users/:id/grant-complimentary', protect, authorize('admin', 'alert-admin'), adminController.grantComplimentaryPro);
router.delete('/users/:id', protect, authorize('admin'), adminController.deleteUser);
router.get('/users/:id/investigations', protect, authorize('admin'), adminController.getUserInvestigations);
router.get('/users/:id/academy-progress', protect, authorize('admin'), adminController.getUserAcademyProgress);
// Seeding Routes (Public temporarily for easy browser access)
router.get('/seed-templates', adminController.seedTemplates);
router.get('/seed-alerts', adminController.seedAlerts);

// Settings Routes
router.get('/settings', protect, authorize('admin'), adminSettingsController.getSettings);
router.put('/settings', protect, authorize('admin'), adminSettingsController.updateSettings);

// Data Exports & Audit
router.get('/exports/users', protect, authorize('admin'), adminSettingsController.exportUsers);
router.get('/exports/investigations', protect, authorize('admin'), adminSettingsController.exportInvestigations);
router.get('/audit-logs', protect, authorize('admin'), adminSettingsController.getAuditLogs);


// SEO Routes
router.get('/seo/pages', protect, authorize('admin'), adminSettingsController.getSeoPages);
router.put('/seo/pages', protect, authorize('admin'), adminSettingsController.updateSeoPage);
router.post('/seo/ping', protect, authorize('admin'), adminSettingsController.pingSitemap);

router.post('/settings/test-email', protect, authorize('admin'), adminSettingsController.testEmail);
router.post('/create-stripe-price', protect, authorize('admin'), adminSettingsController.createStripePrice);
router.post('/sync-stripe', protect, authorize('admin'), adminController.syncStripe);

// Marketing & Cleanup Routes
router.get('/marketing/stats', protect, authorize('admin', 'alert-admin'), adminController.getMarketingStats);
router.post('/marketing/send', protect, authorize('admin'), adminController.sendMarketingEmail);
router.post('/cleanup-unverified', protect, authorize('admin'), adminController.cleanupUnverifiedUsers);

// Email Management Routes
router.get('/emails/logs', protect, authorize('admin'), adminController.getEmailLogs);
router.get('/emails/templates', protect, authorize('admin'), adminController.getEmailTemplates);
router.put('/emails/templates', protect, authorize('admin'), adminController.updateEmailTemplate);
router.post('/emails/test', protect, authorize('admin'), adminController.testEmailTemplate);

router.post('/notifications/send', protect, authorize('admin'), adminController.sendNotification);
router.post('/whitelist-domain', protect, authorize('admin'), adminController.whitelistDomain);
router.get('/student-domains', protect, authorize('admin'), adminController.getStudentDomains);
router.delete('/student-domains/:id', protect, authorize('admin'), adminController.deleteStudentDomain);

// Newsletter Routes
router.post('/newsletter/generate', protect, authorize('admin'), newsletterController.generateNewsletter);
router.post('/newsletter/preview', protect, authorize('admin'), newsletterController.previewNewsletter);
router.post('/newsletter/send', protect, authorize('admin'), newsletterController.sendNewsletter);
router.get('/newsletter/settings', protect, authorize('admin'), newsletterController.getNewsletterSettings);
router.put('/newsletter/settings', protect, authorize('admin'), newsletterController.updateNewsletterSettings);
router.get('/newsletter/subscribers', protect, authorize('admin'), newsletterController.getSubscribers);
router.post('/newsletter/unsubscribe', protect, authorize('admin'), newsletterController.unsubscribeEmail);
router.post('/newsletter/import', protect, authorize('admin'), newsletterController.importSubscribers);
router.get('/beta-signups', protect, authorize('admin', 'alert-admin'), newsletterController.getBetaSignups);
router.delete('/beta-signups/:id', protect, authorize('admin', 'alert-admin'), newsletterController.deleteBetaSignup);

// Manual Blog Generation
router.post('/blog/generate-ai', protect, authorize('admin'), adminController.generateAiBlog);

// Manual Threat Briefing Trigger
router.post('/trigger-briefing', protect, authorize('admin'), adminController.triggerDailyBriefing);

module.exports = router;
