// Temporal-Multiversal FX Parity Law API Routes

import { Router } from 'express';
import { tmfplParityService } from './tmfpl-parity.service';
import { tmfplCorrectionService } from './tmfpl-correction.service';
import { tmfplMonitoringService } from './tmfpl-monitoring.service';

const router = Router();

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

/**
 * @swagger
 * /api/v1/tmfpl/:parityId:
 *   get:
 *     summary: Get parity by ID
 */
router.get('/:parityId', async (req, res, next) => {
  try {
    const parity = await tmfplParityService.getParity(req.params.parityId);
    if (!parity) {
      return res.status(404).json({ error: 'Parity not found' });
    }
    res.json(parity);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/tmfpl/latest/:currencyPair:
 *   get:
 *     summary: Get latest parity for currency pair
 */
router.get('/latest/:currencyPair', async (req, res, next) => {
  try {
    const parity = await tmfplParityService.getLatestParity(req.params.currencyPair);
    if (!parity) {
      return res.status(404).json({ error: 'Parity not found' });
    }
    res.json(parity);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/tmfpl/corrections/required:
 *   get:
 *     summary: Get all parities requiring correction
 */
router.get('/corrections/required', async (req, res, next) => {
  try {
    const parities = await tmfplParityService.getParitiesRequiringCorrection();
    res.json(parities);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/tmfpl/correct/:parityId:
 *   post:
 *     summary: Trigger temporal FX correction
 */
router.post('/correct/:parityId', async (req, res, next) => {
  try {
    const result = await tmfplCorrectionService.triggerTemporalFxCorrection(req.params.parityId);
    res.status(201).json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/tmfpl/correct/all:
 *   post:
 *     summary: Apply corrections to all parities requiring correction
 */
router.post('/correct/all', async (req, res, next) => {
  try {
    const results = await tmfplCorrectionService.applyAllCorrections();
    res.json(results);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/tmfpl/monitor/:parityId:
 *   post:
 *     summary: Monitor parity divergence
 */
router.post('/monitor/:parityId', async (req, res, next) => {
  try {
    const alert = await tmfplMonitoringService.monitorParityDivergence(req.params.parityId);
    res.json(alert);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/tmfpl/monitor/all:
 *   post:
 *     summary: Monitor all parities for divergence
 */
router.post('/monitor/all', async (req, res, next) => {
  try {
    const alerts = await tmfplMonitoringService.monitorAllParities();
    res.json(alerts);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/tmfpl/divergences:
 *   get:
 *     summary: Get all active divergences
 */
router.get('/divergences', async (req, res, next) => {
  try {
    const divergences = await tmfplMonitoringService.getActiveDivergences();
    res.json(divergences);
  } catch (error) {
      return next(error);
  }
});

export default router;

