// Hyper-Sovereign Monetary Nexus API Routes

import { Router } from 'express';
import { hsmnNexusService } from './hsmn-nexus.service';
import { hsmnMultiversalService } from './hsmn-multiversal.service';
import { hsmnTemporalService } from './hsmn-temporal.service';
import { hsmnConsciousnessService } from './hsmn-consciousness.service';
import { hsmnQuantumService } from './hsmn-quantum.service';
import { hsmnBindingService } from './hsmn-binding.service';

const router = Router();

/**
 * @swagger
 * /api/v1/hsmn/nexus/prime:
 *   get:
 *     summary: Get Prime Nexus (HS0)
 */
router.get('/nexus/prime', async (req, res, next) => {
  try {
    const nexus = await hsmnNexusService.getPrimeNexus();
    if (!nexus) {
      return res.status(404).json({ error: 'Prime Nexus not found' });
    }
    res.json(nexus);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/hsmn/nexus/prime:
 *   post:
 *     summary: Initialize Prime Nexus
 */
router.post('/nexus/prime', async (req, res, next) => {
  try {
    const { anchorValue, stabilityIndex } = req.body;
    const nexus = await hsmnNexusService.initializePrimeNexus(anchorValue, stabilityIndex);
    res.status(201).json(nexus);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/hsmn/nexus:
 *   get:
 *     summary: Get all nexus layers
 */
router.get('/nexus', async (req, res, next) => {
  try {
    const layers = await hsmnNexusService.getAllNexusLayers();
    res.json(layers);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/hsmn/nexus/:layerNumber:
 *   get:
 *     summary: Get nexus by layer number
 */
router.get('/nexus/:layerNumber', async (req, res, next) => {
  try {
    const layerNumber = parseInt(req.params.layerNumber);
    const nexus = await hsmnNexusService.getNexusByLayer(layerNumber);
    if (!nexus) {
      return res.status(404).json({ error: 'Nexus not found' });
    }
    res.json(nexus);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/hsmn/multiversal/initialize:
 *   post:
 *     summary: Initialize Multiversal Sovereign Nexus (HS1)
 */
router.post('/multiversal/initialize', async (req, res, next) => {
  try {
    const nexus = await hsmnMultiversalService.initializeMultiversalNexus();
    res.status(201).json(nexus);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/hsmn/multiversal/map:
 *   post:
 *     summary: Map sovereign to parallel reality branch
 */
router.post('/multiversal/map', async (req, res, next) => {
  try {
    const { sovereignBankId, realityBranch, parallelState, identityHash } = req.body;
    const mapping = await hsmnMultiversalService.mapSovereignToBranch(
      sovereignBankId,
      realityBranch,
      parallelState,
      identityHash
    );
    res.status(201).json(mapping);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/hsmn/temporal/initialize:
 *   post:
 *     summary: Initialize Temporal Nexus (HS2)
 */
router.post('/temporal/initialize', async (req, res, next) => {
  try {
    const nexus = await hsmnTemporalService.initializeTemporalNexus();
    res.status(201).json(nexus);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/hsmn/temporal/register:
 *   post:
 *     summary: Register temporal state for sovereign
 */
router.post('/temporal/register', async (req, res, next) => {
  try {
    const { sovereignBankId, timeline, temporalState, consistencyLevel } = req.body;
    const state = await hsmnTemporalService.registerTemporalState(
      sovereignBankId,
      timeline,
      temporalState,
      consistencyLevel
    );
    res.status(201).json(state);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/hsmn/temporal/consistency/:sovereignBankId:
 *   get:
 *     summary: Check temporal consistency
 */
router.get('/temporal/consistency/:sovereignBankId', async (req, res, next) => {
  try {
    const result = await hsmnTemporalService.checkTemporalConsistency(req.params.sovereignBankId);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/hsmn/consciousness/initialize:
 *   post:
 *     summary: Initialize Consciousness Nexus (HS3)
 */
router.post('/consciousness/initialize', async (req, res, next) => {
  try {
    const nexus = await hsmnConsciousnessService.initializeConsciousnessNexus();
    res.status(201).json(nexus);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/hsmn/consciousness/register:
 *   post:
 *     summary: Register consciousness state
 */
router.post('/consciousness/register', async (req, res, next) => {
  try {
    const { agentId, cognitiveState, economicIntent, alignmentScore } = req.body;
    const state = await hsmnConsciousnessService.registerConsciousnessState(
      agentId,
      cognitiveState,
      economicIntent,
      alignmentScore
    );
    res.status(201).json(state);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/hsmn/quantum/initialize:
 *   post:
 *     summary: Initialize Quantum Nexus (HS4)
 */
router.post('/quantum/initialize', async (req, res, next) => {
  try {
    const nexus = await hsmnQuantumService.initializeQuantumNexus();
    res.status(201).json(nexus);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/hsmn/quantum/register:
 *   post:
 *     summary: Register quantum state
 */
router.post('/quantum/register', async (req, res, next) => {
  try {
    const { sovereignBankId, probabilityState, coherenceLevel, superpositionValue } = req.body;
    const state = await hsmnQuantumService.registerQuantumState(
      sovereignBankId,
      probabilityState,
      coherenceLevel,
      superpositionValue
    );
    res.status(201).json(state);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/hsmn/binding/unify:
 *   post:
 *     summary: Unify sovereign identity across all realities
 */
router.post('/binding/unify', async (req, res, next) => {
  try {
    const { sovereignBankId } = req.body;
    const result = await hsmnBindingService.unifySovereignIdentity(sovereignBankId);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/hsmn/binding/status/:sovereignBankId:
 *   get:
 *     summary: Get binding status for sovereign
 */
router.get('/binding/status/:sovereignBankId', async (req, res, next) => {
  try {
    const status = await hsmnBindingService.getBindingStatus(req.params.sovereignBankId);
    res.json(status);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/hsmn/binding/bound:
 *   get:
 *     summary: Get all bound sovereigns
 */
router.get('/binding/bound', async (req, res, next) => {
  try {
    const bound = await hsmnBindingService.getBoundSovereigns();
    res.json(bound);
  } catch (error) {
      return next(error);
  }
});

export default router;

