import express from 'express';
import { isAuthenticated } from '../../keycloak.js';

const router = express.Router();
import {
  listJobsRouteHandler,
  listJobsByPropertiesRouteHandler,
  getJobRouteHandler,
  newJobRouteHandler,
  deleteJobRouteHandler,
  getJobStatsRouteHandler,
} from '../../services/production/jobs.js';
import { convertPropertiesString, getFilter } from '../../utils.js';

// list of jobs
router.get('/', isAuthenticated, (req, res) => {
  const { page, limit, property, search, sort, order } = req.query;
  const allowedFilters = ['state'];
  const filter = getFilter(req.query, allowedFilters);
  listJobsRouteHandler(req, res, page, limit, property, filter, search, sort, order);
});

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

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

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

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

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

export default router;
