// Unified Dimensional Arbitrage Engine API Routes

import { Router } from 'express';
import { udaeEngineService } from './udae-engine.service';
import { udaeCompressionService } from './udae-compression.service';
import { udaeRebalanceService } from './udae-rebalance.service';

const router = Router();

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

/**
 * @swagger
 * /api/v1/udae/:arbitrageId:
 *   get:
 *     summary: Get arbitrage calculation by ID
 */
router.get('/:arbitrageId', async (req, res, next) => {
  try {
    const arbitrage = await udaeEngineService.getArbitrageCalculation(req.params.arbitrageId);
    if (!arbitrage) {
      return res.status(404).json({ error: 'Arbitrage not found' });
    }
    res.json(arbitrage);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/udae/opportunities:
 *   get:
 *     summary: Get all arbitrage opportunities requiring rebalance
 */
router.get('/opportunities', async (req, res, next) => {
  try {
    const opportunities = await udaeEngineService.getArbitrageOpportunities();
    res.json(opportunities);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/udae/dimension/:dimension:
 *   get:
 *     summary: Get arbitrage calculations by dimension
 */
router.get('/dimension/:dimension', async (req, res, next) => {
  try {
    const arbitrages = await udaeEngineService.getArbitrageByDimension(req.params.dimension);
    res.json(arbitrages);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/udae/compress/:arbitrageId:
 *   post:
 *     summary: Check and compress arbitrage
 */
router.post('/compress/:arbitrageId', async (req, res, next) => {
  try {
    const { tolerance } = req.body;
    const result = await udaeCompressionService.checkAndCompress(req.params.arbitrageId, tolerance);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/udae/compress/all:
 *   post:
 *     summary: Compress all arbitrage opportunities
 */
router.post('/compress/all', async (req, res, next) => {
  try {
    const { tolerance } = req.body;
    const results = await udaeCompressionService.compressAllArbitrageOpportunities(tolerance);
    res.json(results);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/udae/rebalance/:arbitrageId:
 *   post:
 *     summary: Execute dimensional rebalance
 */
router.post('/rebalance/:arbitrageId', async (req, res, next) => {
  try {
    const result = await udaeRebalanceService.executeDimensionalRebalance(req.params.arbitrageId);
    res.status(201).json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/udae/rebalance/:rebalanceId:
 *   get:
 *     summary: Get rebalance by ID
 */
router.get('/rebalance/:rebalanceId', async (req, res, next) => {
  try {
    const rebalance = await udaeRebalanceService.getRebalance(req.params.rebalanceId);
    if (!rebalance) {
      return res.status(404).json({ error: 'Rebalance not found' });
    }
    res.json(rebalance);
  } catch (error) {
      return next(error);
  }
});

export default router;

