// Quantum Proxy Server API Routes

import { Router } from 'express';
import { quantumProxyService } from './quantum-proxy.service';
import { quantumEnvelopeService } from './quantum-envelope.service';
import { quantumTranslationService } from './quantum-translation.service';
import { quantumCompatibilityService } from './quantum-compatibility.service';
import { zeroTrustAuthMiddleware } from '@/integration/api-gateway/middleware/auth.middleware';

const router = Router();

/**
 * @route POST /api/quantum-proxy/bridge
 * @desc Bridge transaction from legacy system to DBIS QFS
 */
router.post(
  '/bridge',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await quantumProxyService.bridgeTransaction(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/quantum-proxy/transaction/:proxyTransactionId
 * @desc Get proxy transaction by ID
 */
router.get(
  '/transaction/:proxyTransactionId',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const transaction = await quantumProxyService.getProxyTransaction(
        req.params.proxyTransactionId
      );
      if (!transaction) {
        return res.status(404).json({ error: 'Transaction not found' });
      }
      res.json(transaction);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/quantum-proxy/transactions/bank/:sovereignBankId
 * @desc Get proxy transactions for a bank
 */
router.get(
  '/transactions/bank/:sovereignBankId',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const transactions = await quantumProxyService.getProxyTransactionsForBank(
        req.params.sovereignBankId
      );
      res.json(transactions);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/quantum-proxy/transactions/protocol/:legacyProtocol
 * @desc Get proxy transactions by legacy protocol
 */
router.get(
  '/transactions/protocol/:legacyProtocol',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const limit = parseInt(req.query.limit as string) || 100;
      const transactions = await quantumProxyService.getProxyTransactionsByProtocol(
        req.params.legacyProtocol,
        limit
      );
      res.json(transactions);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/quantum-proxy/envelope/:envelopeId
 * @desc Get quantum envelope by ID
 */
router.get(
  '/envelope/:envelopeId',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const envelope = await quantumEnvelopeService.getEnvelope(
        req.params.envelopeId
      );
      if (!envelope) {
        return res.status(404).json({ error: 'Envelope not found' });
      }
      res.json(envelope);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/quantum-proxy/envelope/:envelopeId/verify
 * @desc Verify envelope integrity
 */
router.post(
  '/envelope/:envelopeId/verify',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const verified = await quantumEnvelopeService.verifyEnvelopeIntegrity(
        req.params.envelopeId
      );
      res.json({ envelopeId: req.params.envelopeId, verified });
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/quantum-proxy/translate
 * @desc Translate legacy transaction to quantum-compatible format
 */
router.post(
  '/translate',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await quantumTranslationService.translateLegacyTransaction(
        req.body
      );
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/quantum-proxy/translation/:translationId
 * @desc Get translation by ID
 */
router.get(
  '/translation/:translationId',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const translation = await quantumTranslationService.getTranslation(
        req.params.translationId
      );
      if (!translation) {
        return res.status(404).json({ error: 'Translation not found' });
      }
      res.json(translation);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/quantum-proxy/compatibility/check
 * @desc Check compatibility of legacy protocol
 */
router.post(
  '/compatibility/check',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await quantumCompatibilityService.checkCompatibility(
        req.body
      );
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/quantum-proxy/compatibility/protocols
 * @desc List all supported protocols
 */
router.get(
  '/compatibility/protocols',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const protocols = await quantumCompatibilityService.listSupportedProtocols();
      res.json({ protocols });
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/quantum-proxy/compatibility/mapping
 * @desc Register protocol mapping
 */
router.post(
  '/compatibility/mapping',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const { legacyProtocol, mappingConfig } = req.body;
      const mappingId = await quantumCompatibilityService.registerProtocolMapping(
        legacyProtocol,
        mappingConfig
      );
      res.json({ mappingId, legacyProtocol });
    } catch (error) {
      return next(error);
    }
  }
);

export default router;

