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

const router = express.Router();
import {
  listSubJobsRouteHandler,
  listSubJobsByPropertiesRouteHandler,
  getSubJobRouteHandler,
  getSubJobStatsRouteHandler,
  getSubJobHistoryRouteHandler,
} from '../../services/production/subjobs.js';
import { getFilter, convertPropertiesString } from '../../utils.js';

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

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

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

// get sub job history
router.get('/history', isAuthenticated, (req, res) => {
  getSubJobHistoryRouteHandler(req, res);
});

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

export default router;
