// GRHS API Routes

import { Router } from 'express';
import { grhsService } from './grhs.service';
import { regulatoryEquivalenceService } from './regulatory-equivalence.service';
import { monetaryHarmonizationService } from './monetary-harmonization.service';
import { legalHarmonizationService } from './legal-harmonization.service';
import { complianceHarmonizationService } from './compliance-harmonization.service';
import { tradeHarmonizationService } from './trade-harmonization.service';

const router = Router();

/**
 * @swagger
 * /api/v1/grhs/initialize:
 *   post:
 *     summary: Initialize GRHS with default rules
 *     tags: [GRHS]
 */
router.post('/initialize', async (req, res, next) => {
  try {
    await grhsService.initialize();
    res.json({ message: 'GRHS initialized successfully' });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/grhs/rep/:sovereignBankId:
 *   get:
 *     summary: Get REP score for sovereign
 *     tags: [GRHS]
 */
router.get('/rep/:sovereignBankId', async (req, res, next) => {
  try {
    const score = await regulatoryEquivalenceService.getREPScore(req.params.sovereignBankId);
    if (!score) {
      // Calculate if not exists
      const newScore = await grhsService.calculateREPScore(req.params.sovereignBankId);
      return res.json(newScore);
    }
    res.json(score);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/grhs/rep/:sovereignBankId/calculate:
 *   post:
 *     summary: Calculate REP score for sovereign
 *     tags: [GRHS]
 */
router.post('/rep/:sovereignBankId/calculate', async (req, res, next) => {
  try {
    const score = await grhsService.calculateREPScore(req.params.sovereignBankId);
    res.json(score);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/grhs/compliance/:sovereignBankId:
 *   get:
 *     summary: Assess compliance across all pillars
 *     tags: [GRHS]
 */
router.get('/compliance/:sovereignBankId', async (req, res, next) => {
  try {
    const compliance = await grhsService.assessCompliance(req.params.sovereignBankId);
    res.json(compliance);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/grhs/fast-track/:sovereignBankId:
 *   post:
 *     summary: Grant fast-track privileges
 *     tags: [GRHS]
 */
router.post('/fast-track/:sovereignBankId', async (req, res, next) => {
  try {
    await grhsService.grantFastTrackPrivileges(req.params.sovereignBankId);
    res.json({ message: 'Fast-track privileges granted' });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/grhs/fast-track/:sovereignBankId/:privilegeType:
 *   get:
 *     summary: Check fast-track privilege
 *     tags: [GRHS]
 */
router.get('/fast-track/:sovereignBankId/:privilegeType', async (req, res, next) => {
  try {
    const hasPrivilege = await grhsService.hasFastTrackPrivilege(
      req.params.sovereignBankId,
      req.params.privilegeType as any
    );
    res.json({ hasPrivilege });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/grhs/rules/monetary:
 *   get:
 *     summary: Get monetary harmonization rules
 *     tags: [GRHS]
 */
router.get('/rules/monetary', async (req, res, next) => {
  try {
    const rules = await monetaryHarmonizationService.getRules();
    res.json(rules);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/grhs/rules/legal:
 *   get:
 *     summary: Get legal harmonization rules
 *     tags: [GRHS]
 */
router.get('/rules/legal', async (req, res, next) => {
  try {
    const rules = await legalHarmonizationService.getRules();
    res.json(rules);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/grhs/rules/compliance:
 *   get:
 *     summary: Get compliance harmonization rules
 *     tags: [GRHS]
 */
router.get('/rules/compliance', async (req, res, next) => {
  try {
    const rules = await complianceHarmonizationService.getRules();
    res.json(rules);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/grhs/rules/trade:
 *   get:
 *     summary: Get trade harmonization rules
 *     tags: [GRHS]
 */
router.get('/rules/trade', async (req, res, next) => {
  try {
    const rules = await tradeHarmonizationService.getRules();
    res.json(rules);
  } catch (error) {
      return next(error);
  }
});

export default router;

