const express = require('express');
const router = express.Router();

const authMiddleware = require('../middleware/authMiddleware');
const adminOnly = require('../middleware/adminOnly');
const AdminUserController = require('../controller/admin/AdminUserController');
const DocumentTemplateController = require('../controller/documentTemplate/DocumentTemplateController');
const CompanyStampController = require('../controller/companyStamp/CompanyStampController');
const CoffeeController = require('../controller/admin/CoffeeController');
const AffiliateController = require('../controller/affiliate/AffiliateController');
const NewsController = require('../controller/news/NewsController');
const PoolController = require('../controller/pool/PoolController');
const I18nPreferencesController = require('../controller/admin/I18nPreferencesController');
const MailTemplatesController = require('../controller/admin/MailTemplatesController');

// Helper middlewares for company-stamp
function forceCompanyForAdmin(req, res, next) {
  if (req.user && ['admin','super_admin'].includes(req.user.role) && req.user.user_type !== 'company') {
    req.user.user_type = 'company';
  }
  next();
}

// DELETE /admin/user/:id (moved from routes/admin.js)
router.delete('/admin/user/:id', authMiddleware, adminOnly, AdminUserController.deleteUser);
router.delete('/admin/mail-templates/:id', authMiddleware, adminOnly, MailTemplatesController.remove);

// DELETE /document-templates/:id (moved from routes/documentTemplates.js)
router.delete('/document-templates/:id', authMiddleware, DocumentTemplateController.deleteTemplate);

// Company-stamp DELETE
router.delete('/company-stamps/:id', authMiddleware, adminOnly, forceCompanyForAdmin, CompanyStampController.delete);
// Admin: delete coffee product
router.delete('/admin/coffee/:id', authMiddleware, adminOnly, CoffeeController.remove);
// Admin: delete affiliate
router.delete('/admin/affiliates/:id', authMiddleware, adminOnly, AffiliateController.delete);

// Admin: delete news
router.delete('/admin/news/:id', authMiddleware, adminOnly, NewsController.delete);

// Admin: remove pool members
router.delete('/admin/pools/:id/members', authMiddleware, adminOnly, PoolController.removeMembers);
router.delete('/admin/i18n/preferences', authMiddleware, adminOnly, I18nPreferencesController.delete);
router.delete('/i18n/preferences', authMiddleware, adminOnly, I18nPreferencesController.delete);

module.exports = router;
