import { Router } from 'express';
import multer from 'multer';
import * as appSettingController from '../controllers/appSetting.controller.js';
import * as backupController from '../controllers/backup.controller.js';
import * as rateLimitAdminController from '../controllers/rateLimitAdmin.controller.js';
import * as blzDataController from '../controllers/blzData.controller.js';
import { authenticate, requirePermission } from '../middleware/auth.js';

// Multer für Backup-Upload (in Memory speichern)
const backupUpload = multer({
  storage: multer.memoryStorage(),
  limits: { fileSize: 500 * 1024 * 1024 }, // 500MB max
  fileFilter: (req, file, cb) => {
    if (file.mimetype === 'application/zip' || file.originalname.endsWith('.zip')) {
      cb(null, true);
    } else {
      cb(new Error('Nur ZIP-Dateien sind erlaubt'));
    }
  },
});

const router = Router();

// Öffentliche Einstellungen (für alle authentifizierten Benutzer, inkl. Kunden)
router.get('/public', authenticate, appSettingController.getPublicSettings);

// Alle Einstellungen (nur Admin)
router.get('/', authenticate, requirePermission('settings:read'), appSettingController.getAllSettings);

// Einzelne Einstellung aktualisieren (nur Admin)
router.put(
  '/:key',
  authenticate,
  requirePermission('settings:update'),
  appSettingController.updateSetting
);

// Mehrere Einstellungen aktualisieren (nur Admin)
router.put(
  '/',
  authenticate,
  requirePermission('settings:update'),
  appSettingController.updateSettings
);

// ==================== BACKUP & RESTORE ====================

// Liste aller Backups
router.get(
  '/backups',
  authenticate,
  requirePermission('settings:update'),
  backupController.listBackups
);

// Neues Backup erstellen
router.post(
  '/backup',
  authenticate,
  requirePermission('settings:update'),
  backupController.createBackup
);

// Backup wiederherstellen
router.post(
  '/backup/:name/restore',
  authenticate,
  requirePermission('settings:update'),
  backupController.restoreBackup
);

// Backup löschen
router.delete(
  '/backup/:name',
  authenticate,
  requirePermission('settings:update'),
  backupController.deleteBackup
);

// Backup als ZIP herunterladen
router.get(
  '/backup/:name/download',
  authenticate,
  requirePermission('settings:update'),
  backupController.downloadBackup
);

// Backup-ZIP hochladen
router.post(
  '/backup/upload',
  authenticate,
  requirePermission('settings:update'),
  backupUpload.single('backup'),
  backupController.uploadBackup
);

// Werkseinstellungen (alles löschen)
router.post(
  '/factory-reset',
  authenticate,
  requirePermission('settings:update'),
  backupController.factoryReset
);

// Backup-Operations-Log: Liste (ohne fullLog) + Detail
router.get(
  '/backup-logs',
  authenticate,
  requirePermission('settings:update'),
  backupController.listBackupLogs
);
router.get(
  '/backup-logs/:id',
  authenticate,
  requirePermission('settings:update'),
  backupController.getBackupLogDetail
);

// ==================== BLZ-/BANKDATEN ====================

// Status (installierte Version, Quelle, Auto-Update-Stand)
router.get(
  '/blz',
  authenticate,
  requirePermission('settings:read'),
  blzDataController.getStatus,
);

// Jetzt prüfen/aktualisieren (?force=1 lädt auch bei gleicher Version)
router.post(
  '/blz/update-now',
  authenticate,
  requirePermission('settings:update'),
  blzDataController.updateNow,
);

// Rate-Limit-Verwaltung (Admin)
router.get(
  '/rate-limits/active',
  authenticate,
  requirePermission('settings:read'),
  rateLimitAdminController.getActiveRateLimits,
);
router.post(
  '/rate-limits/reset',
  authenticate,
  requirePermission('settings:update'),
  rateLimitAdminController.resetRateLimit,
);

export default router;
