// GASE API Routes

import { Router } from 'express';
import { gaseService } from './gase.service';
import { sanctionsSyncService } from './sanctions-sync.service';
import { pepGraphService } from './pep-graph.service';
import { sasCalculatorService } from './sas-calculator.service';
import { riskTieringService } from './risk-tiering.service';

const router = Router();

/**
 * @swagger
 * /api/v1/gase/sanctions/sync:
 *   post:
 *     summary: Synchronize sanctions lists
 *     tags: [GASE]
 */
router.post('/sanctions/sync', async (req, res, next) => {
  try {
    await gaseService.syncSanctionsLists(req.body.sovereignBankId);
    return res.json({ message: 'Sanctions lists synchronized' });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gase/sanctions/search:
 *   post:
 *     summary: Search sanctions list
 *     tags: [GASE]
 */
router.post('/sanctions/search', async (req, res, next) => {
  try {
    const matches = await gaseService.searchSanctions(
      req.body.entityName,
      req.body.threshold
    );
    return res.json(matches);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gase/pep/add:
 *   post:
 *     summary: Add PEP to graph
 *     tags: [GASE]
 */
router.post('/pep/add', async (req, res, next) => {
  try {
    const pep = await gaseService.addPEP(req.body);
    return res.json(pep);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gase/pep/:entityId/connections:
 *   get:
 *     summary: Find PEP connections
 *     tags: [GASE]
 */
router.get('/pep/:entityId/connections', async (req, res, next) => {
  try {
    const connections = await gaseService.findPEPConnections(
      req.params.entityId,
      req.query.maxDepth ? parseInt(req.query.maxDepth as string) : undefined
    );
    return res.json(connections);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gase/sas/calculate:
 *   post:
 *     summary: Calculate Suspicious Activity Score
 *     tags: [GASE]
 */
router.post('/sas/calculate', async (req, res, next) => {
  try {
    const sas = await gaseService.calculateSAS(
      req.body.transactionId,
      req.body.entityId
    );
    return res.json(sas);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gase/sas/:transactionId:
 *   get:
 *     summary: Get SAS for transaction
 *     tags: [GASE]
 */
router.get('/sas/:transactionId', async (req, res, next) => {
  try {
    const sas = await sasCalculatorService.getSAS(req.params.transactionId);
    if (!sas) {
      return res.status(404).json({ error: 'SAS not found' });
    }
    return res.json(sas);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gase/risk-tier/:entityId:
 *   get:
 *     summary: Get risk tier for entity
 *     tags: [GASE]
 */
router.get('/risk-tier/:entityId', async (req, res, next) => {
  try {
    const tier = await gaseService.getRiskTier(req.params.entityId);
    if (!tier) {
      return res.status(404).json({ error: 'Risk tier not found' });
    }
    return res.json({ entityId: req.params.entityId, riskTier: tier });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gase/risk-tier/:entityId/assign:
 *   post:
 *     summary: Assign risk tier to entity
 *     tags: [GASE]
 */
router.post('/risk-tier/:entityId/assign', async (req, res, next) => {
  try {
    const tier = await gaseService.assignRiskTier(req.params.entityId);
    return res.json({ entityId: req.params.entityId, riskTier: tier });
  } catch (error) {
      return next(error);
  }
});

export default router;

