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

const router = express.Router();
import {
  listCourierServicesRouteHandler,
  getCourierServiceRouteHandler,
  editCourierServiceRouteHandler,
  newCourierServiceRouteHandler,
  deleteCourierServiceRouteHandler,
  listCourierServicesByPropertiesRouteHandler,
  getCourierServiceStatsRouteHandler,
  getCourierServiceHistoryRouteHandler,
} from '../../services/management/courierservice.js';

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

router.get('/properties', isAuthenticated, (req, res) => {
  let properties = convertPropertiesString(req.query.properties);
  const allowedFilters = ['courier._id', 'name', 'active', 'deliveryTime', 'courier'];
  const filter = getFilter(req.query, allowedFilters, false);
  listCourierServicesByPropertiesRouteHandler(req, res, properties, filter);
});

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

// get courier service stats
router.get('/stats', isAuthenticated, (req, res) => {
  getCourierServiceStatsRouteHandler(req, res);
});

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

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

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

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

export default router;
