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

const router = express.Router();
import {
  listDocumentJobsRouteHandler,
  getDocumentJobRouteHandler,
  editDocumentJobRouteHandler,
  newDocumentJobRouteHandler,
  deleteDocumentJobRouteHandler,
  listDocumentJobsByPropertiesRouteHandler,
  getDocumentJobStatsRouteHandler,
  getDocumentJobHistoryRouteHandler,
} from '../../services/management/documentjobs.js';

// list of document jobs
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);
  listDocumentJobsRouteHandler(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);
  listDocumentJobsByPropertiesRouteHandler(req, res, properties, filter);
});

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

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

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

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

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

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

export default router;
