// CIM API Routes

import { Router } from 'express';
import { cimIdentityService } from './cim-identity.service';
import { cimInterledgerService } from './cim-interledger.service';
import { cimContractsService } from './cim-contracts.service';
import { cimOfflineService } from './cim-offline.service';

const router = Router();

/**
 * @swagger
 * /api/v1/cim/identity/map:
 *   post:
 *     summary: Map cross-sovereign identity
 *     description: Create identity mapping between sovereign banks for CBDC interoperability
 *     tags: [CBDC, CIM]
 *     security:
 *       - SovereignToken: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - sourceSovereignId
 *               - targetSovereignId
 *               - identityType
 *             properties:
 *               sourceSovereignId:
 *                 type: string
 *                 description: Source sovereign bank ID
 *               targetSovereignId:
 *                 type: string
 *                 description: Target sovereign bank ID
 *               identityType:
 *                 type: string
 *                 enum: [Sovereign, Institutional, Retail, Contract]
 *     responses:
 *       200:
 *         description: Identity mapped successfully
 *       400:
 *         description: Validation error
 *       401:
 *         description: Unauthorized
 */
router.post('/identity/map', async (req, res, next) => {
  try {
    const result = await cimIdentityService.mapIdentity(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/cim/interledger/convert:
 *   post:
 *     summary: CBDC interledger conversion
 *     description: Convert CBDC from one sovereign ledger to another
 *     tags: [CBDC, CIM]
 *     security:
 *       - SovereignToken: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - sourceCbdcId
 *               - targetSovereignId
 *               - amount
 *             properties:
 *               sourceCbdcId:
 *                 type: string
 *               targetSovereignId:
 *                 type: string
 *               amount:
 *                 type: string
 *                 example: "1000.00"
 *     responses:
 *       200:
 *         description: Conversion completed
 *       400:
 *         description: Validation error
 */
router.post('/interledger/convert', async (req, res, next) => {
  try {
    const result = await cimInterledgerService.convertCbdc(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/cim/contracts/templates:
 *   get:
 *     summary: List DBIS-CT templates
 */
router.get('/contracts/templates', async (req, res, next) => {
  try {
    const { templateType } = req.query;
    const templates = await cimContractsService.listContractTemplates(
      templateType as string
    );
    res.json(templates);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/cim/offline/sync-capsule:
 *   post:
 *     summary: Sync offline capsule
 */
router.post('/offline/sync-capsule', async (req, res, next) => {
  try {
    const { capsuleId } = req.body;
    const result = await cimOfflineService.syncCapsuleGlobally(capsuleId);
    res.json({ success: result });
  } catch (error) {
      return next(error);
  }
});

export default router;

