import { Router } from 'express';
import checklistController from '../controllers/checklist.controller';
import { authenticate } from '../middleware/auth.middleware';
import { requirePermission } from '../middleware/rbac.middleware';

const router = Router();

// --- Fälligkeiten (before /:id routes) ---
router.get(
  '/faellig',
  authenticate,
  requirePermission('checklisten:view'),
  checklistController.getOverdueChecklists.bind(checklistController)
);

// --- Overview ---
router.get(
  '/overview',
  authenticate,
  requirePermission('checklisten:view'),
  checklistController.getOverviewItems.bind(checklistController)
);

// --- Vorlagen (Templates) ---
router.get(
  '/vorlagen',
  authenticate,
  requirePermission('checklisten:view'),
  checklistController.getVorlagen.bind(checklistController)
);

router.post(
  '/vorlagen',
  authenticate,
  requirePermission('checklisten:manage_templates'),
  checklistController.createVorlage.bind(checklistController)
);

router.get(
  '/vorlagen/:id',
  authenticate,
  requirePermission('checklisten:view'),
  checklistController.getVorlageById.bind(checklistController)
);

router.put(
  '/vorlagen/:id',
  authenticate,
  requirePermission('checklisten:manage_templates'),
  checklistController.updateVorlage.bind(checklistController)
);

router.delete(
  '/vorlagen/:id',
  authenticate,
  requirePermission('checklisten:manage_templates'),
  checklistController.deleteVorlage.bind(checklistController)
);

// --- Vorlage Items ---
router.get(
  '/vorlagen/:id/items',
  authenticate,
  requirePermission('checklisten:view'),
  checklistController.getVorlageItems.bind(checklistController)
);

router.post(
  '/vorlagen/:id/items',
  authenticate,
  requirePermission('checklisten:manage_templates'),
  checklistController.addVorlageItem.bind(checklistController)
);

// Item-level routes (not nested under vorlage)
router.put(
  '/items/:id',
  authenticate,
  requirePermission('checklisten:manage_templates'),
  checklistController.updateVorlageItem.bind(checklistController)
);

router.delete(
  '/items/:id',
  authenticate,
  requirePermission('checklisten:manage_templates'),
  checklistController.deleteVorlageItem.bind(checklistController)
);

// --- Vehicle-specific items ---
router.get(
  '/fahrzeug/:fahrzeugId/items',
  authenticate,
  requirePermission('checklisten:view'),
  checklistController.getVehicleItems.bind(checklistController)
);

router.post(
  '/fahrzeug/:fahrzeugId/items',
  authenticate,
  requirePermission('checklisten:manage_templates'),
  checklistController.addVehicleItem.bind(checklistController)
);

router.put(
  '/fahrzeug-items/:id',
  authenticate,
  requirePermission('checklisten:manage_templates'),
  checklistController.updateVehicleItem.bind(checklistController)
);

router.delete(
  '/fahrzeug-items/:id',
  authenticate,
  requirePermission('checklisten:manage_templates'),
  checklistController.deleteVehicleItem.bind(checklistController)
);

// --- Applicable checklists for a vehicle ---
router.get(
  '/fahrzeug/:fahrzeugId/checklisten',
  authenticate,
  requirePermission('checklisten:view'),
  checklistController.getTemplatesForVehicle.bind(checklistController)
);

// --- Vehicle due dates ---
router.get(
  '/fahrzeug/:fahrzeugId/faellig',
  authenticate,
  requirePermission('checklisten:view'),
  checklistController.getDueChecklists.bind(checklistController)
);

// --- Equipment-specific items ---
router.get(
  '/equipment/:ausruestungId/vorlagen',
  authenticate,
  requirePermission('checklisten:view'),
  checklistController.getTemplatesForEquipment.bind(checklistController)
);

router.get(
  '/equipment/:ausruestungId/items',
  authenticate,
  requirePermission('checklisten:view'),
  checklistController.getEquipmentItems.bind(checklistController)
);

router.post(
  '/equipment/:ausruestungId/items',
  authenticate,
  requirePermission('checklisten:manage_templates'),
  checklistController.addEquipmentItem.bind(checklistController)
);

router.patch(
  '/equipment/:ausruestungId/items/:itemId',
  authenticate,
  requirePermission('checklisten:manage_templates'),
  checklistController.updateEquipmentItem.bind(checklistController)
);

router.delete(
  '/equipment/:ausruestungId/items/:itemId',
  authenticate,
  requirePermission('checklisten:manage_templates'),
  checklistController.deleteEquipmentItem.bind(checklistController)
);

// --- Ausführungen (Executions) ---
router.get(
  '/ausfuehrungen',
  authenticate,
  requirePermission('checklisten:view'),
  checklistController.getExecutions.bind(checklistController)
);

router.post(
  '/ausfuehrungen',
  authenticate,
  requirePermission('checklisten:execute'),
  checklistController.startExecution.bind(checklistController)
);

router.get(
  '/ausfuehrungen/:id',
  authenticate,
  requirePermission('checklisten:view'),
  checklistController.getExecutionById.bind(checklistController)
);

router.put(
  '/ausfuehrungen/:id',
  authenticate,
  requirePermission('checklisten:execute'),
  checklistController.submitExecution.bind(checklistController)
);

router.post(
  '/ausfuehrungen/:id/freigabe',
  authenticate,
  requirePermission('checklisten:approve'),
  checklistController.approveExecution.bind(checklistController)
);

export default router;
