// RegTech API Routes

import { Router } from 'express';
import { supervisionEngineService } from './supervision-engine.service';
import { dashboardService } from './dashboard.service';
import { sandboxService } from './sandbox.service';

const router = Router();

/**
 * @swagger
 * /api/regtech/monitor/aml:
 *   post:
 *     summary: Monitor AML behaviors
 *     description: Monitor and detect AML (Anti-Money Laundering) behaviors in transactions
 *     tags: [Compliance, RegTech]
 *     security:
 *       - SovereignToken: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - transactionId
 *               - sovereignBankId
 *             properties:
 *               transactionId:
 *                 type: string
 *               sovereignBankId:
 *                 type: string
 *     responses:
 *       200:
 *         description: AML monitoring results
 *       400:
 *         description: Validation error
 */
router.post('/monitor/aml', async (req, res, next) => {
  try {
    const { transactionId, sovereignBankId } = req.body;
    const results = await supervisionEngineService.monitorAMLBehaviors(transactionId, sovereignBankId);
    res.json(results);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/regtech/monitor/velocity:
 *   post:
 *     summary: Monitor transaction velocity
 *     description: Monitor transaction velocity patterns for anomaly detection
 *     tags: [Compliance, RegTech]
 *     security:
 *       - SovereignToken: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - entityId
 *               - timeWindow
 *             properties:
 *               entityId:
 *                 type: string
 *               timeWindow:
 *                 type: string
 *                 description: Time window in hours
 *                 example: "24"
 *     responses:
 *       200:
 *         description: Velocity analysis results
 */
router.post('/monitor/velocity', async (req, res, next) => {
  try {
    const { sovereignBankId, entityId, timeWindowMinutes } = req.body;
    const results = await supervisionEngineService.monitorTransactionVelocity(
      sovereignBankId,
      entityId,
      timeWindowMinutes
    );
    res.json(results);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/regtech/dashboard/sri:
 *   get:
 *     summary: Get SRI dashboard
 */
router.get('/dashboard/sri', async (req, res, next) => {
  try {
    const sovereignBankId = req.query.sovereignBankId as string | undefined;
    const dashboard = await dashboardService.getSRIDashboard(sovereignBankId);
    res.json(dashboard);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/regtech/dashboard/liquidity:
 *   get:
 *     summary: Get liquidity stress dashboard
 */
router.get('/dashboard/liquidity', async (req, res, next) => {
  try {
    const sovereignBankId = req.query.sovereignBankId as string | undefined;
    const dashboard = await dashboardService.getLiquidityStressDashboard(sovereignBankId);
    res.json(dashboard);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/regtech/dashboard/comprehensive:
 *   get:
 *     summary: Get comprehensive dashboard
 */
router.get('/dashboard/comprehensive', async (req, res, next) => {
  try {
    const sovereignBankId = req.query.sovereignBankId as string | undefined;
    const dashboard = await dashboardService.getComprehensiveDashboard(sovereignBankId);
    res.json(dashboard);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/regtech/sandbox:
 *   post:
 *     summary: Create sandbox scenario
 */
router.post('/sandbox', async (req, res, next) => {
  try {
    const { sovereignBankId, scenario } = req.body;
    const sandbox = await sandboxService.createSandboxScenario(sovereignBankId, scenario);
    res.status(201).json(sandbox);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/regtech/sandbox/:sandboxId/run:
 *   post:
 *     summary: Run sandbox scenario
 */
router.post('/sandbox/:sandboxId/run', async (req, res, next) => {
  try {
    const result = await sandboxService.runSandboxScenario(req.params.sandboxId);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

export default router;

