// M-RTGS API Routes

import { Router } from 'express';
import { mrtgsQueueManagerService } from './mrtgs-queue-manager.service';
import { mrtgsSettlementService } from './mrtgs-settlement.service';
import { mrtgsRiskMonitorService } from './mrtgs-risk-monitor.service';

const router = Router();

/**
 * @swagger
 * /api/v1/m-rtgs/queue/add:
 *   post:
 *     summary: Add payment to queue
 */
router.post('/queue/add', async (req, res, next) => {
  try {
    const result = await mrtgsQueueManagerService.addToQueue(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/m-rtgs/queue/next:
 *   get:
 *     summary: Get next payment from queue
 */
router.get('/queue/next', async (req, res, next) => {
  try {
    const result = await mrtgsQueueManagerService.getNextFromQueue();
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/m-rtgs/settle:
 *   post:
 *     summary: Process settlement
 */
router.post('/settle', async (req, res, next) => {
  try {
    const result = await mrtgsSettlementService.processSettlement(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/m-rtgs/risk/monitor:
 *   post:
 *     summary: Monitor risk
 */
router.post('/risk/monitor', async (req, res, next) => {
  try {
    const { type, ...params } = req.body;
    let result;

    switch (type) {
      case 'velocity':
        result = await mrtgsRiskMonitorService.monitorVelocity(
          params.entityId,
          params.entityType,
          params.timeWindow
        );
        break;
      case 'liquidity':
        result = await mrtgsRiskMonitorService.monitorLiquidityCongestion(
          params.currencyCode,
          params.assetType
        );
        break;
      default:
        throw new Error(`Unknown risk type: ${type}`);
    }

    res.json(result);
  } catch (error) {
      return next(error);
  }
});

export default router;

