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

const router = express.Router();
import {
  listPrintersRouteHandler,
  editPrinterRouteHandler,
  getPrinterRouteHandler,
  newPrinterRouteHandler,
  getPrinterStatsRouteHandler,
  listPrintersByPropertiesRouteHandler,
  getPrinterHistoryRouteHandler,
} from '../../services/production/printers.js';
import { convertPropertiesString, getFilter } from '../../utils.js';

// list of printers
router.get('/', isAuthenticated, (req, res) => {
  const { page, limit, property, search, sort, order } = req.query;
  const allowedFilters = ['tags', 'host._id', 'active', 'online', 'name'];
  const filter = getFilter(req.query, allowedFilters);
  listPrintersRouteHandler(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);
  listPrintersByPropertiesRouteHandler(req, res, properties, filter);
});

// create new printer
router.post('/', isAuthenticated, (req, res) => {
  newPrinterRouteHandler(req, res);
});

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

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

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

// update printer info
router.put('/:id', isAuthenticated, async (req, res) => {
  editPrinterRouteHandler(req, res);
});

export default router;
