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

const router = express.Router();
import {
  listGCodeFilesRouteHandler,
  editGCodeFileRouteHandler,
  getGCodeFileRouteHandler,
  newGCodeFileRouteHandler,
  listGCodeFilesByPropertiesRouteHandler,
  getGCodeFileContentRouteHandler,
  getGCodeFileStatsRouteHandler,
} from '../../services/production/gcodefiles.js';
import { convertPropertiesString, getFilter } from '../../utils.js';

// list of gcodeFiles
router.get('/', isAuthenticated, (req, res) => {
  const { page, limit, property, search, sort, order } = req.query;
  const allowedFilters = ['_id', 'name', 'filament._id'];
  const filter = getFilter(req.query, allowedFilters);
  listGCodeFilesRouteHandler(req, res, page, limit, property, filter, search, sort, order);
});

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

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

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

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

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

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

export default router;
