import express from 'express';
import { isAuthenticated } from '../../keycloak.js';
import {
  listNotesRouteHandler,
  getNoteRouteHandler,
  editNoteRouteHandler,
  newNoteRouteHandler,
  deleteNoteRouteHandler,
  listNotesByPropertiesRouteHandler,
  getNoteStatsRouteHandler,
  getNoteHistoryRouteHandler,
} from '../../services/misc/notes.js';
import { getFilter, convertPropertiesString } from '../../utils.js';

const router = express.Router();

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

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

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

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

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

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

// update note info
router.put('/:id', isAuthenticated, async (req, res) => {
  editNoteRouteHandler(req, res);
});
// Delete note
router.delete('/:id', isAuthenticated, deleteNoteRouteHandler);

export default router;
