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 UserSettingsController = require('../controller/auth/UserSettingsController');
const DocumentTemplateController = require('../controller/documentTemplate/DocumentTemplateController');
const CoffeeController = require('../controller/admin/CoffeeController');
const CompanySettingsController = require('../controller/admin/CompanySettingsController');
const DashboardPlatformsController = require('../controller/admin/DashboardPlatformsController');
const ShippingFeesController = require('../controller/admin/ShippingFeesController');
const I18nPreferencesController = require('../controller/admin/I18nPreferencesController');
const multer = require('multer');
const upload = multer({ storage: multer.memoryStorage() });

// PUT /admin/users/:id/permissions (moved from routes/admin.js)
router.put('/admin/users/:id/permissions', authMiddleware, adminOnly, AdminUserController.updateUserPermissions);

// PUT /document-templates/:id (moved from routes/documentTemplates.js)
router.put('/document-templates/:id', authMiddleware, upload.single('file'), DocumentTemplateController.updateTemplate);

// User: update persisted UI settings
router.put('/user/settings', authMiddleware, UserSettingsController.updateSettings);
router.put('/settings', authMiddleware, UserSettingsController.updateSettings);

// Admin: update coffee product (supports picture file replacement)
router.put('/admin/coffee/:id', authMiddleware, adminOnly, upload.single('picture'), CoffeeController.update);

// Admin: update company settings (invoice address etc.)
router.put('/admin/company-settings', authMiddleware, adminOnly, CompanySettingsController.update);

// Admin: update dashboard platform
router.put('/admin/dashboard-platforms/:id', authMiddleware, adminOnly, DashboardPlatformsController.update);

// Admin: update shipping fee for a piece count (60/120)
router.put('/admin/shipping-fees/:pieceCount', authMiddleware, adminOnly, ShippingFeesController.updatePrice);
router.put('/admin/i18n/preferences', authMiddleware, adminOnly, I18nPreferencesController.put);
router.put('/i18n/preferences', authMiddleware, adminOnly, I18nPreferencesController.put);
router.put('/i18n/translations', authMiddleware, adminOnly, I18nPreferencesController.upsertTranslations);

module.exports = router;
