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

const authMiddleware = require('../middleware/authMiddleware');
const adminOnly = require('../middleware/adminOnly');
const DocumentTemplateController = require('../controller/documentTemplate/DocumentTemplateController');
const CompanyStampController = require('../controller/companyStamp/CompanyStampController'); // <-- added
const CoffeeController = require('../controller/admin/CoffeeController');
const AdminUserController = require('../controller/admin/AdminUserController');
const PersonalProfileController = require('../controller/profile/PersonalProfileController'); // <-- add
const PoolController = require('../controller/pool/PoolController'); // <-- new
const MatrixController = require('../controller/matrix/MatrixController'); // <-- new
const AffiliateController = require('../controller/affiliate/AffiliateController'); // <-- new
const NewsController = require('../controller/news/NewsController');
const AbonemmentController = require('../controller/abonemments/AbonemmentController');
const InvoiceController = require('../controller/invoice/InvoiceController');
const DashboardPlatformsController = require('../controller/admin/DashboardPlatformsController');
const MailTemplatesController = require('../controller/admin/MailTemplatesController');

const multer = require('multer');
const upload = multer({ storage: multer.memoryStorage() });

// 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();
}

// PATCH /document-templates/:id/state (existing)
router.patch('/document-templates/:id/state', authMiddleware, DocumentTemplateController.updateTemplateState);

// Company-stamp PATCH (activate)
router.patch('/company-stamps/:id/activate', authMiddleware, adminOnly, forceCompanyForAdmin, CompanyStampController.activate);

// Admin user management PATCH routes
router.patch('/admin/archive-user/:id', authMiddleware, adminOnly, AdminUserController.archiveUser);
router.patch('/admin/unarchive-user/:id', authMiddleware, adminOnly, AdminUserController.unarchiveUser);
router.patch('/admin/mail-templates/:id', authMiddleware, adminOnly, MailTemplatesController.update);
router.patch('/admin/mail-templates/:id/activate', authMiddleware, adminOnly, MailTemplatesController.activate);
router.patch('/admin/mail-templates/:id/archive', authMiddleware, adminOnly, MailTemplatesController.archive);
router.patch('/admin/mail-templates/:id/unarchive', authMiddleware, adminOnly, MailTemplatesController.unarchive);
router.patch('/admin/update-verification/:id', authMiddleware, adminOnly, AdminUserController.updateUserVerification);
router.patch('/admin/update-user-profile/:id', authMiddleware, adminOnly, AdminUserController.updateUserProfile);
router.patch('/admin/update-user-status/:id', authMiddleware, adminOnly, AdminUserController.updateUserStatus);
// Admin: set state for coffee product
router.patch('/admin/coffee/:id/state', authMiddleware, adminOnly, CoffeeController.setState);
// Admin: edit coffee gallery pictures (upload/remove/replace)
router.patch('/admin/coffee/:id/pictures', authMiddleware, adminOnly, upload.any(), CoffeeController.editPictures);
// NEW: Admin pool active status update
router.patch('/admin/pools/:id/active', authMiddleware, adminOnly, PoolController.updateActive);
// NEW: Admin update pool linked subscription
router.patch('/admin/pools/:id/subscription', authMiddleware, adminOnly, PoolController.updateSubscription);
// NEW: deactivate a matrix instance (admin-only)
router.patch('/admin/matrix/:id/deactivate', authMiddleware, adminOnly, MatrixController.deactivate);
// NEW: activate a matrix instance (admin-only)
router.patch('/admin/matrix/:id/activate', authMiddleware, adminOnly, MatrixController.activate);
// NEW: Update affiliate (with optional logo upload)
router.patch('/admin/affiliates/:id', authMiddleware, adminOnly, upload.single('logo'), AffiliateController.update);
// NEW: Update affiliate status only
router.patch('/admin/affiliates/:id/status', authMiddleware, adminOnly, AffiliateController.updateStatus);

// News Manager
router.patch('/admin/news/:id', authMiddleware, adminOnly, upload.single('image'), NewsController.update);
router.patch('/admin/news/:id/status', authMiddleware, adminOnly, NewsController.updateStatus);

// Admin: update invoice status
router.patch('/admin/invoices/:id/status', authMiddleware, adminOnly, InvoiceController.updateStatus);

// Admin: dashboard platforms state
router.patch('/admin/dashboard-platforms/:id/state', authMiddleware, adminOnly, DashboardPlatformsController.setState);

// Personal profile (self-service) - no admin guard
router.patch('/profile/personal/basic', authMiddleware, PersonalProfileController.updateBasic);
router.patch('/profile/personal/bank', authMiddleware, PersonalProfileController.updateBank);
router.patch('/abonements/:id/content', authMiddleware, AbonemmentController.updateContent);

// Add other PATCH routes here as needed

module.exports = router;
