// Global Banking Identity Graph (GBIG) - API Routes

import { Router } from 'express';
import { gbigService } from './gbig.service';
import { IdentityLayer, IdentityType, RelationshipType, KYCLevel } from './types';

const router = Router();

/**
 * @swagger
 * tags:
 *   name: Identity
 *   description: Global Banking Identity Graph (GBIG)
 */

/**
 * @swagger
 * /api/gbig/identities:
 *   post:
 *     summary: Create a new identity node
 *     description: Create a new identity node in the Global Banking Identity Graph
 *     tags: [Identity]
 *     security:
 *       - SovereignToken: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - identityType
 *               - layer
 *               - kycLevel
 *               - sovereignBankId
 *             properties:
 *               identityType:
 *                 type: string
 *               layer:
 *                 type: string
 *               kycLevel:
 *                 type: string
 *               sovereignBankId:
 *                 type: string
 *     responses:
 *       200:
 *         description: Identity node created
 */
router.post('/identities', async (req, res) => {
  try {
    const { identityType, layer, kycLevel, sovereignBankId, metadata } = req.body;

    const node = await gbigService.createIdentityNode(
      identityType as IdentityType,
      layer as IdentityLayer,
      kycLevel as KYCLevel,
      sovereignBankId,
      metadata
    );

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

/**
 * POST /api/gbig/edges
 * Create a relationship edge
 */
router.post('/edges', async (req, res) => {
  try {
    const { fromNodeId, toNodeId, relationshipType, authorizationLevel, metadata } =
      req.body;

    const edge = await gbigService.createIdentityEdge(
      fromNodeId,
      toNodeId,
      relationshipType as RelationshipType,
      authorizationLevel,
      metadata
    );

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

/**
 * POST /api/gbig/trust-score
 * Update trust score for an identity
 */
router.post('/trust-score', async (req, res) => {
  try {
    const { identityId, factors } = req.body;

    const trustScore = await gbigService.updateTrustScore(identityId, factors);

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

/**
 * POST /api/gbig/verify
 * Verify an identity
 */
router.post('/verify', async (req, res) => {
  try {
    const result = await gbigService.verifyIdentity(req.body);

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

/**
 * GET /api/gbig/graph
 * Get the complete identity graph
 */
router.get('/graph', async (req, res) => {
  try {
    const { sovereignBankId } = req.query;

    const graph = await gbigService.getGraph(
      sovereignBankId as string | undefined
    );

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

/**
 * POST /api/gbig/scb/register
 * Register a Sovereign Central Bank
 */
router.post('/scb/register', async (req, res) => {
  try {
    const { scbId, certificateHash, masterKeyId, hsmSignature } = req.body;

    const node = await gbigService.registerSCB(
      scbId,
      certificateHash,
      masterKeyId,
      hsmSignature
    );

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

/**
 * POST /api/gbig/contract/register
 * Register a smart contract identity
 */
router.post('/contract/register', async (req, res) => {
  try {
    const { contractHashId, sovereignSignatoryList, executionPermissions } = req.body;

    const node = await gbigService.registerContract(
      contractHashId,
      sovereignSignatoryList,
      executionPermissions
    );

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

export default router;

