// MRLI API Routes

import { Router } from 'express';
import { mrliInterfaceService } from './mrli-interface.service';
import { mrliSyncService } from './mrli-sync.service';
import { mrliConflictService } from './mrli-conflict.service';

const router = Router();

/**
 * @swagger
 * /api/v1/mrli/ledger:
 *   post:
 *     summary: Create multi-reality ledger
 */
router.post('/ledger', async (req, res, next) => {
  try {
    const ledger = await mrliInterfaceService.createLedger(req.body);
    res.json(ledger);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/mrli/ledger/:ledgerId:
 *   get:
 *     summary: Get multi-reality ledger
 */
router.get('/ledger/:ledgerId', async (req, res, next) => {
  try {
    const ledger = await mrliInterfaceService.getLedger(req.params.ledgerId);
    if (!ledger) {
      return res.status(404).json({ error: 'Ledger not found' });
    }
    res.json(ledger);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/mrli/interface/ci:
 *   post:
 *     summary: Create classical interface
 */
router.post('/interface/ci', async (req, res, next) => {
  try {
    const result = await mrliInterfaceService.createCI(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/mrli/interface/dli:
 *   post:
 *     summary: Create distributed ledger interface
 */
router.post('/interface/dli', async (req, res, next) => {
  try {
    const result = await mrliInterfaceService.createDLI(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/mrli/interface/qli:
 *   post:
 *     summary: Create quantum ledger interface
 */
router.post('/interface/qli', async (req, res, next) => {
  try {
    const result = await mrliInterfaceService.createQLI(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/mrli/interface/si:
 *   post:
 *     summary: Create simulation interface
 */
router.post('/interface/si', async (req, res, next) => {
  try {
    const result = await mrliInterfaceService.createSI(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/mrli/sync:
 *   post:
 *     summary: Synchronize multi-reality ledger states
 */
router.post('/sync', async (req, res, next) => {
  try {
    const result = await mrliSyncService.synchronize(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/mrli/sync/:syncId:
 *   get:
 *     summary: Get synchronization
 */
router.get('/sync/:syncId', async (req, res, next) => {
  try {
    const sync = await mrliSyncService.getSync(req.params.syncId);
    if (!sync) {
      return res.status(404).json({ error: 'Synchronization not found' });
    }
    res.json(sync);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/mrli/conflict/:syncId/resolve:
 *   post:
 *     summary: Resolve conflict using DBIS arbitration
 */
router.post('/conflict/:syncId/resolve', async (req, res, next) => {
  try {
    const result = await mrliConflictService.resolveConflictByArbitration(req.params.syncId);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

export default router;

