// CSSE API Routes

import { Router } from 'express';
import { csseEngineService } from './csse-engine.service';
import { cssePrecommitService } from './csse-precommit.service';
import { csseReconciliationService } from './csse-reconciliation.service';

const router = Router();

// Settlement routes
router.post('/settlements', async (req, res, next) => {
  try {
    const settlement = await csseEngineService.createSettlement(req.body);
    res.json(settlement);
  } catch (error) {
      return next(error);
  }
});

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

router.get('/settlements/:settlementId', async (req, res, next) => {
  try {
    const settlement = await csseEngineService.getSettlement(req.params.settlementId);
    res.json(settlement);
  } catch (error) {
      return next(error);
  }
});

router.post('/settlements/:settlementId/execute', async (req, res, next) => {
  try {
    const settlement = await csseEngineService.executeSettlement(req.params.settlementId);
    res.json(settlement);
  } catch (error) {
      return next(error);
  }
});

// Pre-commit routes
router.post('/precommit/:settlementId', async (req, res, next) => {
  try {
    const preCommit = await cssePrecommitService.createPreCommit(req.params.settlementId, req.body);
    res.json(preCommit);
  } catch (error) {
      return next(error);
  }
});

router.post('/precommit/:preCommitId/verify', async (req, res, next) => {
  try {
    const preCommit = await cssePrecommitService.verifyPreCommit(req.params.preCommitId);
    res.json(preCommit);
  } catch (error) {
      return next(error);
  }
});

router.post('/precommit/:preCommitId/commit', async (req, res, next) => {
  try {
    const preCommit = await cssePrecommitService.commitPreCommit(req.params.preCommitId);
    res.json(preCommit);
  } catch (error) {
      return next(error);
  }
});

router.get('/precommit/:preCommitId', async (req, res, next) => {
  try {
    const preCommit = await cssePrecommitService.getPreCommit(req.params.preCommitId);
    res.json(preCommit);
  } catch (error) {
      return next(error);
  }
});

// Reconciliation routes
router.post('/reconciliation/:settlementId', async (req, res, next) => {
  try {
    const reconciliation = await csseReconciliationService.createReconciliation(
      req.params.settlementId,
      req.body
    );
    res.json(reconciliation);
  } catch (error) {
      return next(error);
  }
});

router.post('/reconciliation/:settlementId/calculate', async (req, res, next) => {
  try {
    const reconciliation = await csseReconciliationService.calculateReconciliation(req.params.settlementId);
    res.json(reconciliation);
  } catch (error) {
      return next(error);
  }
});

router.post('/reconciliation/:reconciliationId/apply', async (req, res, next) => {
  try {
    const reconciliation = await csseReconciliationService.applyReconciliation(req.params.reconciliationId);
    res.json(reconciliation);
  } catch (error) {
      return next(error);
  }
});

router.get('/reconciliation/:settlementId', async (req, res, next) => {
  try {
    const reconciliations = await csseReconciliationService.getReconciliationsForSettlement(req.params.settlementId);
    res.json(reconciliations);
  } catch (error) {
      return next(error);
  }
});

export default router;

