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

const router = express.Router();
import {
  listNoteTypesRouteHandler,
  getNoteTypeRouteHandler,
  editNoteTypeRouteHandler,
  newNoteTypeRouteHandler,
  deleteNoteTypeRouteHandler,
  listNoteTypesByPropertiesRouteHandler,
  getNoteTypeStatsRouteHandler,
  getNoteTypeHistoryRouteHandler,
} from '../../services/management/notetypes.js';

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

router.get('/properties', isAuthenticated, (req, res) => {
  let properties = convertPropertiesString(req.query.properties);
  const allowedFilters = ['active', 'color'];
  const filter = getFilter(req.query, allowedFilters, false);
  var masterFilter = {};
  if (req.query.masterFilter) {
    masterFilter = JSON.parse(req.query.masterFilter);
  }
  listNoteTypesByPropertiesRouteHandler(req, res, properties, filter, masterFilter);
});

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

// get note type stats
router.get('/stats', isAuthenticated, (req, res) => {
  getNoteTypeStatsRouteHandler(req, res);
});

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

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

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

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

export default router;
