// ISN API Routes

import { Router } from 'express';
import { isnRoutingService } from './isn-routing.service';
import { smartClearingService } from './smart-clearing.service';
import { atomicSettlementService } from './atomic-settlement.service';

const router = Router();

/**
 * @swagger
 * /api/isn/route:
 *   post:
 *     summary: Find optimal settlement route
 *     description: Calculate optimal route for instant settlement using SIRE algorithm
 *     tags: [Settlement, ISN]
 *     security:
 *       - SovereignToken: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - sourceBankId
 *               - destinationBankId
 *               - amount
 *               - currencyCode
 *             properties:
 *               sourceBankId:
 *                 type: string
 *               destinationBankId:
 *                 type: string
 *               amount:
 *                 type: string
 *               currencyCode:
 *                 type: string
 *     responses:
 *       200:
 *         description: Route calculated
 *       400:
 *         description: Validation error
 */
router.post('/route', async (req, res, next) => {
  try {
    const route = await isnRoutingService.findOptimalRoute(req.body);
    res.json(route);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/isn/netting:
 *   post:
 *     summary: Net bilateral obligations
 *     description: Perform netting of bilateral payment obligations between banks
 *     tags: [Settlement, ISN]
 *     security:
 *       - SovereignToken: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - bank1Id
 *               - bank2Id
 *             properties:
 *               bank1Id:
 *                 type: string
 *               bank2Id:
 *                 type: string
 *     responses:
 *       200:
 *         description: Netting completed
 */
router.post('/netting', async (req, res, next) => {
  try {
    const { bank1Id, bank2Id, currencyCode } = req.body;
    const result = await smartClearingService.netBilateralObligations(bank1Id, bank2Id, currencyCode);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/isn/multi-legged:
 *   post:
 *     summary: Execute multi-legged transaction
 */
router.post('/multi-legged', async (req, res, next) => {
  try {
    const { legs } = req.body;
    const result = await smartClearingService.executeMultiLeggedTransaction(legs);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/isn/atomic:
 *   post:
 *     summary: Execute atomic settlement
 */
router.post('/atomic', async (req, res, next) => {
  try {
    const result = await atomicSettlementService.executeAtomicSettlement(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/isn/settlements/:settlementId:
 *   get:
 *     summary: Get settlement by ID
 */
router.get('/settlements/:settlementId', async (req, res, next) => {
  try {
    const settlement = await atomicSettlementService.getSettlement(req.params.settlementId);
    if (!settlement) {
      return res.status(404).json({ error: 'Settlement not found' });
    }
    res.json(settlement);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/isn/settlements/bank/:sovereignBankId:
 *   get:
 *     summary: Get settlements for bank
 */
router.get('/settlements/bank/:sovereignBankId', async (req, res, next) => {
  try {
    const limit = parseInt(req.query.limit as string) || 100;
    const settlements = await atomicSettlementService.getSettlementsForBank(req.params.sovereignBankId, limit);
    res.json(settlements);
  } catch (error) {
      return next(error);
  }
});

export default router;

