// Global CBDC Tokenomics Framework (GCTF) - API Routes

import { Router } from 'express';
import { gctfService } from './gctf.service';
import { CBDCUnitType } from './types';

const router = Router();

/**
 * @swagger
 * tags:
 *   name: CBDC Tokenomics
 *   description: Global CBDC Tokenomics Framework (GCTF)
 */

/**
 * @swagger
 * /api/gctf/issue:
 *   post:
 *     summary: Issue new CBDC tokens
 *     description: Issue new CBDC tokens through the Global CBDC Tokenomics Framework
 *     tags: [CBDC Tokenomics]
 *     security:
 *       - SovereignToken: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - amount
 *               - sovereignBankId
 *             properties:
 *               amount:
 *                 type: string
 *               sovereignBankId:
 *                 type: string
 *     responses:
 *       200:
 *         description: CBDC tokens issued
 */
router.post('/issue', async (req, res) => {
  try {
    const adjustment = await gctfService.issueCBDC(req.body);

    res.json(adjustment);
  } catch (error) {
    res.status(500).json({ error: (error as Error).message });
  }
});

/**
 * POST /api/gctf/burn
 * Burn CBDC tokens
 */
router.post('/burn', async (req, res) => {
  try {
    const adjustment = await gctfService.burnCBDC(req.body);

    res.json(adjustment);
  } catch (error) {
    res.status(500).json({ error: (error as Error).message });
  }
});

/**
 * GET /api/gctf/supply-mechanics/:sovereignBankId/:unitType
 * Get supply mechanics for a CBDC unit
 */
router.get('/supply-mechanics/:sovereignBankId/:unitType', async (req, res) => {
  try {
    const { sovereignBankId, unitType } = req.params;

    const mechanics = await gctfService.getSupplyMechanics(
      sovereignBankId,
      unitType as CBDCUnitType
    );

    res.json(mechanics);
  } catch (error) {
    res.status(500).json({ error: (error as Error).message });
  }
});

/**
 * POST /api/gctf/velocity-controls
 * Configure velocity controls
 */
router.post('/velocity-controls', async (req, res) => {
  try {
    const { sovereignBankId, unitType, ...controls } = req.body;

    const velocityControl = await gctfService.configureVelocityControls(
      sovereignBankId,
      unitType as CBDCUnitType,
      controls
    );

    res.json(velocityControl);
  } catch (error) {
    res.status(500).json({ error: (error as Error).message });
  }
});

/**
 * POST /api/gctf/quarantine/enable
 * Enable sovereign quarantine mode
 */
router.post('/quarantine/enable', async (req, res) => {
  try {
    const { sovereignBankId, unitType, endDate } = req.body;

    const controls = await gctfService.enableQuarantineMode(
      sovereignBankId,
      unitType as CBDCUnitType,
      endDate ? new Date(endDate) : undefined
    );

    res.json(controls);
  } catch (error) {
    res.status(500).json({ error: (error as Error).message });
  }
});

/**
 * POST /api/gctf/quarantine/disable
 * Disable sovereign quarantine mode
 */
router.post('/quarantine/disable', async (req, res) => {
  try {
    const { sovereignBankId, unitType } = req.body;

    const controls = await gctfService.disableQuarantineMode(
      sovereignBankId,
      unitType as CBDCUnitType
    );

    res.json(controls);
  } catch (error) {
    res.status(500).json({ error: (error as Error).message });
  }
});

/**
 * GET /api/gctf/config/:sovereignBankId/:unitType
 * Get tokenomics configuration
 */
router.get('/config/:sovereignBankId/:unitType', async (req, res) => {
  try {
    const { sovereignBankId, unitType } = req.params;

    const config = await gctfService.getTokenomicsConfig(
      sovereignBankId,
      unitType as CBDCUnitType
    );

    res.json(config);
  } catch (error) {
    res.status(500).json({ error: (error as Error).message });
  }
});

/**
 * POST /api/gctf/calculate-issuance
 * Calculate CBDC issuance using the rule
 */
router.post('/calculate-issuance', async (req, res) => {
  try {
    const { demandAdjustment, settlementVolume, factor, stressPenalties } = req.body;

    const rule = gctfService.calculateIssuanceRule(
      demandAdjustment,
      settlementVolume,
      factor,
      stressPenalties
    );

    res.json(rule);
  } catch (error) {
    res.status(500).json({ error: (error as Error).message });
  }
});

/**
 * POST /api/gctf/check-velocity
 * Check if a transaction is allowed under velocity controls
 */
router.post('/check-velocity', async (req, res) => {
  try {
    const { sovereignBankId, unitType, transactionAmount, category } = req.body;

    const result = await gctfService.checkVelocityControls(
      sovereignBankId,
      unitType as CBDCUnitType,
      transactionAmount,
      category
    );

    res.json(result);
  } catch (error) {
    res.status(500).json({ error: (error as Error).message });
  }
});

export default router;

