// PSG API Routes

import { Router } from 'express';
import { psgArchitectureService } from './psg-architecture.service';
import { psgEpochEngineService } from './psg-epoch-engine.service';
import { psgRelayHubsService } from './psg-relay-hubs.service';

const router = Router();

router.post('/nodes', async (req, res, next) => {
  try {
    const node = await psgArchitectureService.createGeoRedundantNode(req.body);
    res.status(201).json(node);
  } catch (error) {
      return next(error);
  }
});

router.post('/epochs', async (req, res, next) => {
  try {
    const epoch = await psgEpochEngineService.createEpoch(
      req.body.assetType,
      req.body.commitData,
      req.body.nodeId
    );
    res.json(epoch);
  } catch (error) {
      return next(error);
  }
});

router.get('/epochs', async (req, res, next) => {
  try {
    const epochs = await psgEpochEngineService.getRecentEpochs(
      req.query.assetType as string,
      parseInt(req.query.limit as string) || 100
    );
    res.json(epochs);
  } catch (error) {
      return next(error);
  }
});

router.post('/relay-hubs', async (req, res, next) => {
  try {
    const hub = await psgRelayHubsService.createRelayHub(req.body);
    res.status(201).json(hub);
  } catch (error) {
      return next(error);
  }
});

router.post('/optimize-route', async (req, res, next) => {
  try {
    const route = await psgRelayHubsService.getOptimalRoute(
      req.body.source,
      req.body.destination
    );
    res.json(route);
  } catch (error) {
      return next(error);
  }
});

export default router;

