const express = require('express');
const router = express.Router();
const cutsController = require('../controllers/cutsController');
const { requireAuth, requireAdmin } = require('../middleware/auth');
const config = require('../config');

// Add middleware to check if cuts table is configured
const checkCutsTable = (req, res, next) => {
    if (!config.nocodb.cutsSheetId) {
        console.warn('Cuts table not configured - NOCODB_CUTS_SHEET not set');
        // Continue anyway, controller will handle it
    }
    next();
};

// Apply the check to all routes
router.use(checkCutsTable);

// Get all cuts (filtered by permissions)
router.get('/', cutsController.getAll);

// Get single cut by ID
router.get('/:id', cutsController.getById);

// Admin only routes
router.post('/', requireAdmin, cutsController.create);
router.put('/:id', requireAdmin, cutsController.update);
router.delete('/:id', requireAdmin, cutsController.delete);

// New cut enhancement routes - admin only
router.get('/:id/locations', requireAdmin, cutsController.getLocationsInCut);
router.get('/:id/locations/export', requireAdmin, cutsController.exportCutLocations);
router.get('/:id/statistics', requireAdmin, cutsController.getCutStatistics);
router.put('/:id/settings', requireAdmin, cutsController.updateCutSettings);

module.exports = router;
