// ILC API Routes

import { Router } from 'express';
import { ilcInterfaceService } from './ilc-interface.service';
import { ilcDimensionService } from './ilc-dimension.service';
import { ilcConsistencyService } from './ilc-consistency.service';

const router = Router();

// Ledger routes
router.post('/ledgers', async (req, res, next) => {
  try {
    const ledger = await ilcInterfaceService.createLedger(req.body);
    res.json(ledger);
  } catch (error) {
      return next(error);
  }
});

router.get('/ledgers', async (req, res, next) => {
  try {
    const ledgers = await ilcInterfaceService.getAllLedgers(req.query);
    res.json(ledgers);
  } catch (error) {
      return next(error);
  }
});

router.get('/ledgers/:ledgerId', async (req, res, next) => {
  try {
    const ledger = await ilcInterfaceService.getLedger(req.params.ledgerId);
    res.json(ledger);
  } catch (error) {
      return next(error);
  }
});

router.put('/ledgers/:ledgerId/state', async (req, res, next) => {
  try {
    const ledger = await ilcInterfaceService.updateLedgerState(req.params.ledgerId, req.body);
    res.json(ledger);
  } catch (error) {
      return next(error);
  }
});

// Dimension routes
router.post('/dimensions/:ledgerId', async (req, res, next) => {
  try {
    const dimension = await ilcDimensionService.createDimension(req.params.ledgerId, req.body);
    res.json(dimension);
  } catch (error) {
      return next(error);
  }
});

router.get('/dimensions/:ledgerId', async (req, res, next) => {
  try {
    const dimensions = await ilcDimensionService.getDimensionsForLedger(req.params.ledgerId);
    res.json(dimensions);
  } catch (error) {
      return next(error);
  }
});

router.post('/dimensions/:ledgerId/initialize', async (req, res, next) => {
  try {
    const dimensions = await ilcDimensionService.initializeStandardDimensions(req.params.ledgerId);
    res.json(dimensions);
  } catch (error) {
      return next(error);
  }
});

// Consistency routes
router.post('/consistency/:ledgerId/check', async (req, res, next) => {
  try {
    const result = await ilcConsistencyService.checkConsistency(req.params.ledgerId);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

router.post('/consistency/:ledgerId/maintain', async (req, res, next) => {
  try {
    const ledger = await ilcConsistencyService.maintainIntegrity(req.params.ledgerId);
    res.json(ledger);
  } catch (error) {
      return next(error);
  }
});

router.get('/consistency/:ledgerId/reconciliations', async (req, res, next) => {
  try {
    const reconciliations = await ilcConsistencyService.getReconciliations(req.params.ledgerId);
    res.json(reconciliations);
  } catch (error) {
      return next(error);
  }
});

export default router;

