// SRI API Routes

import { Router } from 'express';
import { sriCalculatorService } from './sri-calculator.service';
import { sriMonitorService } from './sri-monitor.service';
import { sriEnforcementService } from './sri-enforcement.service';

const router = Router();

/**
 * @swagger
 * /api/sri/calculate:
 *   post:
 *     summary: Calculate Sovereign Risk Index (SRI)
 *     description: Calculate SRI score for a sovereign bank based on multiple risk factors
 *     tags: [Risk, SRI]
 *     security:
 *       - SovereignToken: []
 *     parameters:
 *       - in: path
 *         name: sovereignBankId
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: SRI calculated successfully
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 sriId:
 *                   type: string
 *                 sriScore:
 *                   type: number
 *                 sriRating:
 *                   type: string
 *       400:
 *         description: Validation error
 */
router.post('/calculate/:sovereignBankId', async (req, res, next) => {
  try {
    const result = await sriCalculatorService.createSRI(req.params.sovereignBankId);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/sri/monitor:
 *   get:
 *     summary: Monitor SRI for sovereign bank
 *     description: Get current SRI status and trigger levels
 *     tags: [Risk, SRI]
 *     security:
 *       - SovereignToken: []
 *     parameters:
 *       - in: query
 *         name: sovereignBankId
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: SRI monitoring data
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 currentSRI:
 *                   type: number
 *                 rating:
 *                   type: string
 *                 triggers:
 *                   type: array
 *                   items:
 *                     type: string
 */
router.get('/monitor/:sovereignBankId', async (req, res, next) => {
  try {
    const result = await sriMonitorService.monitorSRI(req.params.sovereignBankId);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/sri/monitor/all:
 *   get:
 *     summary: Monitor all banks
 */
router.get('/monitor/all', async (req, res, next) => {
  try {
    const results = await sriMonitorService.monitorAllBanks();
    res.json(results);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/sri/history/:sovereignBankId:
 *   get:
 *     summary: Get SRI history
 */
router.get('/history/:sovereignBankId', async (req, res, next) => {
  try {
    const limit = parseInt(req.query.limit as string) || 30;
    const history = await sriMonitorService.getSRIHistory(req.params.sovereignBankId, limit);
    res.json(history);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/sri/enforcements:
 *   get:
 *     summary: Get active enforcements
 */
router.get('/enforcements/:sovereignBankId', async (req, res, next) => {
  try {
    const enforcements = await sriEnforcementService.getActiveEnforcements(req.params.sovereignBankId);
    res.json(enforcements);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/sri/high-risk:
 *   get:
 *     summary: Get high risk banks
 */
router.get('/high-risk', async (req, res, next) => {
  try {
    const threshold = parseFloat(req.query.threshold as string) || 40;
    const banks = await sriMonitorService.getHighRiskBanks(threshold);
    res.json(banks);
  } catch (error) {
      return next(error);
  }
});

export default router;

