/**
 * @swagger
 * tags:
 *   name: Metaverse
 *   description: MetaverseDubai Integration Framework - Supplement B
 */

// Metaverse API Routes
// Supplement B: MetaverseDubai Integration Framework

import { Router } from 'express';
import { metaverseNodeService } from './metaverse-node.service';
import { metaverseSettlementService } from './metaverse-settlement.service';
import { metaverseIdentityService } from './metaverse-identity.service';
import { metaverseFxService } from './metaverse-fx.service';
import { metaverseBridgeService } from './metaverse-bridge.service';
import { zeroTrustAuthMiddleware } from '@/integration/api-gateway/middleware/auth.middleware';

// New services
import dsezRoutes from './d-sez/d-sez.routes';
import { metaverseSettlementPipelineService } from './settlement/metaverse-settlement-pipeline.service';
import { onRampService } from './ramps/on-ramp.service';
import { offRampService } from './ramps/off-ramp.service';
import { gpuEdgeIntegrationService } from './compute/gpu-edge-integration.service';
import { nodeTypeManagerService } from './compute/node-type-manager.service';
import { sixGFabricService } from './compute/6g-fabric.service';
import { zkVerificationService } from './compute/zk-verification.service';
import { holographicRenderingService } from './compute/holographic-rendering.service';
import { assetTokenizationService } from './tokenization/asset-tokenization.service';
import { tokenClassManagerService } from './tokenization/token-class-manager.service';
import { crossMetaverseFxService } from './interoperability/cross-metaverse-fx.service';
import { multiDsezBridgeService } from './interoperability/multi-d-sez-bridge.service';
import { realitySpanningService } from './interoperability/reality-spanning.service';
import { multiverseConsistencyService } from './consistency/multiverse-consistency.service';
import { identityMappingService } from './identity/identity-mapping.service';
import { avatarIdentityAnchorService } from './identity/avatar-identity-anchor.service';

const router = Router();

/**
 * @route POST /api/metaverse/node
 * @desc Create Metaverse Economic Node
 */
router.post(
  '/node',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await metaverseNodeService.createMetaverseNode(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/metaverse/node/:nodeId
 * @desc Get metaverse node by ID
 */
router.get(
  '/node/:nodeId',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const node = await metaverseNodeService.getMetaverseNode(
        req.params.nodeId
      );
      if (!node) {
        return res.status(404).json({ error: 'Node not found' });
      }
      res.json(node);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/metaverse/nodes
 * @desc Get all metaverse nodes
 */
router.get(
  '/nodes',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const nodes = await metaverseNodeService.getAllMetaverseNodes();
      res.json(nodes);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/settlement
 * @desc Execute virtual land settlement
 */
router.post(
  '/settlement',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await metaverseSettlementService.executeVirtualLandSettlement(
        req.body
      );
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/metaverse/settlement/:settlementId
 * @desc Get settlement by ID
 */
router.get(
  '/settlement/:settlementId',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const settlement = await metaverseSettlementService.getSettlement(
        req.params.settlementId
      );
      if (!settlement) {
        return res.status(404).json({ error: 'Settlement not found' });
      }
      res.json(settlement);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/identity
 * @desc Create avatar-based identity
 */
router.post(
  '/identity',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await metaverseIdentityService.createAvatarIdentity(
        req.body
      );
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/metaverse/identity/:identityId
 * @desc Get identity by ID
 */
router.get(
  '/identity/:identityId',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const identity = await metaverseIdentityService.getIdentity(
        req.params.identityId
      );
      if (!identity) {
        return res.status(404).json({ error: 'Identity not found' });
      }
      res.json(identity);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/fx
 * @desc Execute inter-metaverse FX
 */
router.post(
  '/fx',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await metaverseFxService.executeInterMetaverseFx(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/bridge
 * @desc Create digital-physical bridge
 */
router.post(
  '/bridge',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await metaverseBridgeService.createBridge(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/metaverse/bridge/:bridgeId
 * @desc Get bridge by ID
 */
router.get(
  '/bridge/:bridgeId',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const bridge = await metaverseBridgeService.getBridge(
        req.params.bridgeId
      );
      if (!bridge) {
        return res.status(404).json({ error: 'Bridge not found' });
      }
      res.json(bridge);
    } catch (error) {
      return next(error);
    }
  }
);

// D-SEZ routes
router.use('/', dsezRoutes);

/**
 * @route POST /api/metaverse/settlement/pipeline
 * @desc Execute full settlement pipeline
 */
router.post(
  '/settlement/pipeline',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await metaverseSettlementPipelineService.executeSettlementPipeline(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/ramp/on
 * @desc Execute on-ramp (Real-world → Metaverse)
 */
router.post(
  '/ramp/on',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await onRampService.executeOnRamp(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/ramp/off
 * @desc Execute off-ramp (Metaverse → Real-world)
 */
router.post(
  '/ramp/off',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await offRampService.executeOffRamp(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/compute/gpu-edge
 * @desc Allocate GPU edge node
 */
router.post(
  '/compute/gpu-edge',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await gpuEdgeIntegrationService.allocateGpuEdgeNode(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/compute/node
 * @desc Create compute node
 */
router.post(
  '/compute/node',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await nodeTypeManagerService.createComputeNode(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/compute/6g
 * @desc Connect to 6G fabric
 */
router.post(
  '/compute/6g',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await sixGFabricService.connectToSixGFabric(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/compute/zk-verify
 * @desc Verify asset exchange using ZK
 */
router.post(
  '/compute/zk-verify',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await zkVerificationService.verifyAssetExchange(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/compute/holographic
 * @desc Initiate holographic rendering
 */
router.post(
  '/compute/holographic',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await holographicRenderingService.initiateRendering(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/tokenization/asset
 * @desc Tokenize asset
 */
router.post(
  '/tokenization/asset',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await assetTokenizationService.tokenizeAsset(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/tokenization/token-class
 * @desc Create token class
 */
router.post(
  '/tokenization/token-class',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await tokenClassManagerService.createTokenClass(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/interoperability/cross-metaverse-fx
 * @desc Execute cross-metaverse FX
 */
router.post(
  '/interoperability/cross-metaverse-fx',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await crossMetaverseFxService.executeCrossMetaverseFx(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/interoperability/multi-d-sez-bridge
 * @desc Create multi-D-SEZ bridge
 */
router.post(
  '/interoperability/multi-d-sez-bridge',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await multiDsezBridgeService.createMultiDsezBridge(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/interoperability/reality-spanning
 * @desc Execute reality-spanning transaction
 */
router.post(
  '/interoperability/reality-spanning',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await realitySpanningService.executeRealitySpanning(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/consistency/check
 * @desc Perform multiverse consistency check
 */
router.post(
  '/consistency/check',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await multiverseConsistencyService.performConsistencyCheck(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/identity/mapping
 * @desc Create identity mapping
 */
router.post(
  '/identity/mapping',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await identityMappingService.createIdentityMapping(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/identity/anchor
 * @desc Anchor avatar identity to ILIE
 */
router.post(
  '/identity/anchor',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await avatarIdentityAnchorService.anchorAvatarIdentity(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

export default router;

