// Global Multiversal Monetary Theory API Routes

import { Router } from 'express';
import { gmmtUnitsService } from './gmmt-units.service';
import { gmmtValuationService } from './gmmt-valuation.service';
import { gmmtConversionService } from './gmmt-conversion.service';
import { gmmtStabilityService } from './gmmt-stability.service';

const router = Router();

/**
 * @swagger
 * /api/v1/gmmt/reality-layers:
 *   get:
 *     summary: Get all reality layers
 */
router.get('/reality-layers', async (req, res, next) => {
  try {
    const layers = await gmmtUnitsService.getRealityLayers();
    res.json(layers);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gmmt/reality-layers:
 *   post:
 *     summary: Create reality layer
 */
router.post('/reality-layers', async (req, res, next) => {
  try {
    const layer = await gmmtUnitsService.createRealityLayer(req.body.layerName, req.body.layerType);
    res.status(201).json(layer);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gmmt/units:
 *   post:
 *     summary: Create monetary unit
 */
router.post('/units', async (req, res, next) => {
  try {
    const unit = await gmmtUnitsService.createUnit(req.body);
    res.status(201).json(unit);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gmmt/units/pmu:
 *   get:
 *     summary: Get Prime Monetary Unit
 */
router.get('/units/pmu', async (req, res, next) => {
  try {
    const pmu = await gmmtUnitsService.getPMU();
    if (!pmu) {
      return res.status(404).json({ error: 'PMU not found' });
    }
    res.json(pmu);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gmmt/valuations:
 *   post:
 *     summary: Calculate valuation
 */
router.post('/valuations', async (req, res, next) => {
  try {
    const valuation = await gmmtValuationService.calculateValuation(req.body);
    res.status(201).json(valuation);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gmmt/conversions:
 *   post:
 *     summary: Create conversion
 */
router.post('/conversions', async (req, res, next) => {
  try {
    const conversion = await gmmtConversionService.createConversion(req.body);
    res.status(201).json(conversion);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gmmt/conversions/convert:
 *   post:
 *     summary: Convert amount between units
 */
router.post('/conversions/convert', async (req, res, next) => {
  try {
    const { sourceUnitId, targetUnitId, amount } = req.body;
    const result = await gmmtConversionService.convertAmount(sourceUnitId, targetUnitId, amount);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gmmt/stability/report:
 *   get:
 *     summary: Get stability report
 */
router.get('/stability/report', async (req, res, next) => {
  try {
    const report = await gmmtStabilityService.getStabilityReport();
    res.json(report);
  } catch (error) {
      return next(error);
  }
});

export default router;

