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

const router = express.Router();
import {
  listTaxRecordsRouteHandler,
  getTaxRecordRouteHandler,
  editTaxRecordRouteHandler,
  newTaxRecordRouteHandler,
  deleteTaxRecordRouteHandler,
  listTaxRecordsByPropertiesRouteHandler,
  getTaxRecordStatsRouteHandler,
  getTaxRecordHistoryRouteHandler,
} from '../../services/management/taxrecords.js';

// list of tax records
router.get('/', isAuthenticated, (req, res) => {
  const { page, limit, property, search, sort, order } = req.query;
  const allowedFilters = ['taxRate', 'transactionType', 'transaction', 'transactionDate'];
  const filter = getFilter(req.query, allowedFilters);
  listTaxRecordsRouteHandler(req, res, page, limit, property, filter, search, sort, order);
});

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

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

// get tax record stats
router.get('/stats', isAuthenticated, (req, res) => {
  getTaxRecordStatsRouteHandler(req, res);
});

// get tax record history
router.get('/history', isAuthenticated, (req, res) => {
  getTaxRecordHistoryRouteHandler(req, res);
});

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

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

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

export default router;
