import { Router } from 'express';
import * as creditNoteController from '../controllers/creditNote.controller.js';
import { authenticate, requirePermission } from '../middleware/auth.js';

const router = Router();

// Gesamtübersicht aller Belege (Hauptmenü) – portal-scoped (nicht staff-only).
// VOR /:id, damit die Wurzel nicht als ID interpretiert wird.
router.get('/', authenticate, requirePermission('contracts:read'), creditNoteController.listAll);

// Nummernkreis-Verwaltung (Einstellungen). VOR /:id, damit "number-range"
// nicht als ID interpretiert wird.
router.get('/number-range', authenticate, requirePermission('settings:read'), creditNoteController.getNumberRange);
router.put('/number-range', authenticate, requirePermission('settings:update'), creditNoteController.updateNumberRange);
router.get('/delivery-note-number-range', authenticate, requirePermission('settings:read'), creditNoteController.getDeliveryNoteNumberRange);
router.put('/delivery-note-number-range', authenticate, requirePermission('settings:update'), creditNoteController.updateDeliveryNoteNumberRange);

// Einzelne Gutschrift
router.post('/:id/pdf', authenticate, requirePermission('contracts:update'), creditNoteController.generatePdf);
router.get('/:id', authenticate, requirePermission('contracts:read'), creditNoteController.getOne);
router.put('/:id', authenticate, requirePermission('contracts:update'), creditNoteController.update);
router.delete('/:id', authenticate, requirePermission('contracts:delete'), creditNoteController.remove);

export default router;
