import express from 'express';
import { isAuthenticated } from '../../keycloak.js';
import { getFilter, convertPropertiesString } from '../../utils.js';

const router = express.Router();
import {
  listDocumentPrintersRouteHandler,
  getDocumentPrinterRouteHandler,
  editDocumentPrinterRouteHandler,
  newDocumentPrinterRouteHandler,
  deleteDocumentPrinterRouteHandler,
  listDocumentPrintersByPropertiesRouteHandler,
  getDocumentPrinterStatsRouteHandler,
  getDocumentPrinterHistoryRouteHandler,
} from '../../services/management/documentprinters.js';

// list of document printers
router.get('/', isAuthenticated, (req, res) => {
  const { page, limit, property, search, sort, order } = req.query;
  const allowedFilters = ['name', 'tags', 'active', 'isGlobal'];
  const filter = getFilter(req.query, allowedFilters);
  listDocumentPrintersRouteHandler(req, res, page, limit, property, filter, search, sort, order);
});

router.get('/properties', isAuthenticated, (req, res) => {
  let properties = convertPropertiesString(req.query.properties);
  const allowedFilters = ['tags'];
  const filter = getFilter(req.query, allowedFilters, false);
  listDocumentPrintersByPropertiesRouteHandler(req, res, properties, filter);
});

router.post('/', isAuthenticated, (req, res) => {
  newDocumentPrinterRouteHandler(req, res);
});

// get document printer stats
router.get('/stats', isAuthenticated, (req, res) => {
  getDocumentPrinterStatsRouteHandler(req, res);
});

// get documentprinters history
router.get('/history', isAuthenticated, (req, res) => {
  getDocumentPrinterHistoryRouteHandler(req, res);
});

router.get('/:id', isAuthenticated, (req, res) => {
  getDocumentPrinterRouteHandler(req, res);
});

router.put('/:id', isAuthenticated, async (req, res) => {
  editDocumentPrinterRouteHandler(req, res);
});

router.delete('/:id', isAuthenticated, async (req, res) => {
  deleteDocumentPrinterRouteHandler(req, res);
});

export default router;
