// Meta-Reality Economic Convergence Protocol API Routes

import { Router } from 'express';
import { mrecpConvergenceService } from './mrecp-convergence.service';
import { mrecpHarmonizationService } from './mrecp-harmonization.service';

const router = Router();

/**
 * @swagger
 * /api/v1/mrecp/calculate:
 *   post:
 *     summary: Calculate convergence
 */
router.post('/calculate', async (req, res, next) => {
  try {
    const result = await mrecpConvergenceService.calculateConvergence(req.body);
    res.status(201).json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/mrecp/:convergenceId:
 *   get:
 *     summary: Get convergence by ID
 */
router.get('/:convergenceId', async (req, res, next) => {
  try {
    const convergence = await mrecpConvergenceService.getConvergence(req.params.convergenceId);
    if (!convergence) {
      return res.status(404).json({ error: 'Convergence not found' });
    }
    res.json(convergence);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/mrecp/stable:
 *   get:
 *     summary: Get stable convergences
 */
router.get('/stable', async (req, res, next) => {
  try {
    const convergences = await mrecpConvergenceService.getStableConvergences();
    res.json(convergences);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/mrecp/minimize/:realityId:
 *   post:
 *     summary: Minimize reality divergence
 */
router.post('/minimize/:realityId', async (req, res, next) => {
  try {
    const { targetDivergence } = req.body;
    const result = await mrecpConvergenceService.minimizeRealityDivergence(
      req.params.realityId,
      targetDivergence
    );
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/mrecp/harmonize/:convergenceId:
 *   post:
 *     summary: Apply harmonization
 */
router.post('/harmonize/:convergenceId', async (req, res, next) => {
  try {
    const result = await mrecpHarmonizationService.applyHarmonization(req.params.convergenceId);
    res.status(201).json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/mrecp/harmonize/all:
 *   post:
 *     summary: Harmonize all unstable convergences
 */
router.post('/harmonize/all', async (req, res, next) => {
  try {
    const results = await mrecpHarmonizationService.harmonizeAllUnstable();
    res.json(results);
  } catch (error) {
      return next(error);
  }
});

export default router;

