// ISP API Routes

import { Router } from 'express';
import { ispNodeService } from './isp-node.service';
import { ispRelayService } from './isp-relay.service';
import { ispSettlementService } from './isp-settlement.service';
import { ispTemporalService } from './isp-temporal.service';
import { ispIssuanceService } from './isp-issuance.service';

const router = Router();

/**
 * @swagger
 * /api/v1/isp/node:
 *   post:
 *     summary: Create interplanetary node
 */
router.post('/node', async (req, res, next) => {
  try {
    const result = await ispNodeService.createNode(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/isp/node/:nodeId:
 *   get:
 *     summary: Get interplanetary node
 */
router.get('/node/:nodeId', async (req, res, next) => {
  try {
    const node = await ispNodeService.getNode(req.params.nodeId);
    if (!node) {
      return res.status(404).json({ error: 'Node not found' });
    }
    res.json(node);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/isp/nodes:
 *   get:
 *     summary: List interplanetary nodes
 */
router.get('/nodes', async (req, res, next) => {
  try {
    const { planetaryLocation } = req.query;
    const nodes = await ispNodeService.listNodes(planetaryLocation as string | undefined);
    res.json(nodes);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/isp/relay:
 *   post:
 *     summary: Send relay message
 */
router.post('/relay', async (req, res, next) => {
  try {
    const result = await ispRelayService.sendMessage(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/isp/relay/:relayId:
 *   get:
 *     summary: Get relay message
 */
router.get('/relay/:relayId', async (req, res, next) => {
  try {
    const relay = await ispRelayService.getRelay(req.params.relayId);
    if (!relay) {
      return res.status(404).json({ error: 'Relay not found' });
    }
    res.json(relay);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/isp/settlement:
 *   post:
 *     summary: Execute interplanetary settlement
 */
router.post('/settlement', async (req, res, next) => {
  try {
    const result = await ispSettlementService.executeSettlement(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/isp/settlement/:settlementId/finalize:
 *   post:
 *     summary: Finalize settlement
 */
router.post('/settlement/:settlementId/finalize', async (req, res, next) => {
  try {
    await ispSettlementService.finalizeSettlement(req.params.settlementId);
    res.json({ status: 'finalized' });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/isp/tse:
 *   post:
 *     summary: Create temporal settlement engine pre-commitment
 */
router.post('/tse', async (req, res, next) => {
  try {
    const result = await ispTemporalService.createTSE(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/isp/tse/:tseId/verify:
 *   post:
 *     summary: Verify TSE pre-commitment
 */
router.post('/tse/:tseId/verify', async (req, res, next) => {
  try {
    const { futureStateEstimate } = req.body;
    const verified = await ispTemporalService.verifyTSE(req.params.tseId, futureStateEstimate);
    res.json({ verified });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/isp/issuance/issu:
 *   post:
 *     summary: Issue Interplanetary SSU
 */
router.post('/issuance/issu', async (req, res, next) => {
  try {
    const result = await ispIssuanceService.issueISSU(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/isp/issuance/icbdc:
 *   post:
 *     summary: Issue Interplanetary CBDC
 */
router.post('/issuance/icbdc', async (req, res, next) => {
  try {
    const result = await ispIssuanceService.issueICBDC(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

export default router;

