// NCE API Routes

import { Router } from 'express';
import { nceEngineService } from './nce-engine.service';
import { nceNeuralService } from './nce-neural.service';
import { nceQuantumService } from './nce-quantum.service';
import { nceStateService } from './nce-state.service';

const router = Router();

// Consensus routes
router.post('/consensus', async (req, res, next) => {
  try {
    const state = await nceEngineService.createConsensus(req.body);
    res.json(state);
  } catch (error) {
      return next(error);
  }
});

router.get('/consensus', async (req, res, next) => {
  try {
    const states = await nceEngineService.getConsensusStates(req.query);
    res.json(states);
  } catch (error) {
      return next(error);
  }
});

router.get('/consensus/:stateId', async (req, res, next) => {
  try {
    const state = await nceEngineService.getConsensusState(req.params.stateId);
    res.json(state);
  } catch (error) {
      return next(error);
  }
});

router.post('/consensus/:stateId/confirm', async (req, res, next) => {
  try {
    const state = await nceEngineService.confirmConsensus(req.params.stateId);
    res.json(state);
  } catch (error) {
      return next(error);
  }
});

// Neural layer routes
router.get('/layers/:stateId', async (req, res, next) => {
  try {
    const layers = await nceNeuralService.getLayers(req.params.stateId);
    res.json(layers);
  } catch (error) {
      return next(error);
  }
});

// Quantum signature routes
router.post('/signatures', async (req, res, next) => {
  try {
    const signature = await nceQuantumService.createQuantumSignature(
      req.body.stateId,
      req.body.quantumKeyId,
      req.body.signatureType
    );
    res.json(signature);
  } catch (error) {
      return next(error);
  }
});

router.get('/signatures/:stateId', async (req, res, next) => {
  try {
    const signatures = await nceQuantumService.getSignaturesForState(req.params.stateId);
    res.json(signatures);
  } catch (error) {
      return next(error);
  }
});

// State validation routes
router.post('/state/validate', async (req, res, next) => {
  try {
    const valid = await nceStateService.validateStateIntegrity(req.body.ledgerStateHash);
    res.json({ valid });
  } catch (error) {
      return next(error);
  }
});

router.post('/state/verify', async (req, res, next) => {
  try {
    const result = await nceStateService.verifyStateBlock(
      req.body.ledgerStateHash,
      req.body.previousHash
    );
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

export default router;

