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

const router = express.Router();
import {
  listDocumentSizesRouteHandler,
  getDocumentSizeRouteHandler,
  editDocumentSizeRouteHandler,
  newDocumentSizeRouteHandler,
  deleteDocumentSizeRouteHandler,
  listDocumentSizesByPropertiesRouteHandler,
  getDocumentSizeStatsRouteHandler,
  getDocumentSizeHistoryRouteHandler,
} from '../../services/management/documentsizes.js';

// list of document sizes
router.get('/', isAuthenticated, (req, res) => {
  const { page, limit, property, search, sort, order } = req.query;
  const allowedFilters = ['name', 'width', 'height'];
  const filter = getFilter(req.query, allowedFilters);
  listDocumentSizesRouteHandler(req, res, page, limit, property, filter, search, sort, order);
});

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

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

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

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

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

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

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

export default router;
