/**
 * @swagger
 * tags:
 *   name: Risk Rules Engine
 *   description: Configurable risk and fraud detection rules
 */

import { Router } from 'express';
import { zeroTrustAuthMiddleware } from '@/integration/api-gateway/middleware/auth.middleware';
import { riskRulesEngine, RiskAssessmentRequest, RiskRule } from './rules-engine.service';

const router = Router();

/**
 * @swagger
 * /api/v1/risk/assess:
 *   post:
 *     summary: Assess risk for a transaction
 *     tags: [Risk Rules Engine]
 *     security:
 *       - SovereignToken: []
 */
router.post('/assess', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const request = req.body as RiskAssessmentRequest;
    const result = await riskRulesEngine.assessRisk(request);
    res.json(result);
  } catch (error) {
    next(error);
  }
});

/**
 * @swagger
 * /api/v1/risk/rules:
 *   get:
 *     summary: Get all risk rules
 *     tags: [Risk Rules Engine]
 *     security:
 *       - SovereignToken: []
 */
router.get('/rules', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const rules = await riskRulesEngine.getActiveRules();
    res.json(rules);
  } catch (error) {
    next(error);
  }
});

/**
 * @swagger
 * /api/v1/risk/rules:
 *   post:
 *     summary: Create a risk rule
 *     tags: [Risk Rules Engine]
 *     security:
 *       - SovereignToken: []
 */
router.post('/rules', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const rule = req.body as Omit<RiskRule, 'id' | 'createdAt' | 'updatedAt'>;
    const created = await riskRulesEngine.createRule(rule);
    res.status(201).json(created);
  } catch (error) {
    next(error);
  }
});

export default router;
