// Temporal Liquidity Portals API Routes

import { Router } from 'express';
import { tlpPortalService } from './tlp-portal.service';
import { tlpLiquidityService } from './tlp-liquidity.service';
import { tlpParadoxDetectionService } from './tlp-paradox-detection.service';
import { tlpBufferService } from './tlp-buffer.service';

const router = Router();

/**
 * @swagger
 * /api/v1/tlp/portals:
 *   get:
 *     summary: Get active portals
 */
router.get('/portals', async (req, res, next) => {
  try {
    const portals = await tlpPortalService.getActivePortals();
    res.json(portals);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/tlp/portals:
 *   post:
 *     summary: Create portal
 */
router.post('/portals', async (req, res, next) => {
  try {
    const portal = await tlpPortalService.createPortal(req.body);
    res.status(201).json(portal);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/tlp/portals/:portalId:
 *   get:
 *     summary: Get portal by ID
 */
router.get('/portals/:portalId', async (req, res, next) => {
  try {
    const portal = await tlpPortalService.getPortal(req.params.portalId);
    if (!portal) {
      return res.status(404).json({ error: 'Portal not found' });
    }
    res.json(portal);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/tlp/liquidity/reserves:
 *   post:
 *     summary: Create future liquidity reserve
 */
router.post('/liquidity/reserves', async (req, res, next) => {
  try {
    const reserve = await tlpLiquidityService.createFutureReserve(req.body);
    res.status(201).json(reserve);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/tlp/liquidity/borrow:
 *   post:
 *     summary: Borrow liquidity
 */
router.post('/liquidity/borrow', async (req, res, next) => {
  try {
    const result = await tlpLiquidityService.borrowLiquidity(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/tlp/liquidity/available/:portalId:
 *   get:
 *     summary: Get available liquidity
 */
router.get('/liquidity/available/:portalId', async (req, res, next) => {
  try {
    const liquidity = await tlpLiquidityService.getAvailableLiquidity(req.params.portalId);
    res.json(liquidity);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/tlp/paradox/detect:
 *   post:
 *     summary: Detect paradox
 */
router.post('/paradox/detect', async (req, res, next) => {
  try {
    const detection = await tlpParadoxDetectionService.detectParadox(req.body);
    res.status(201).json(detection);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/tlp/paradox/unresolved:
 *   get:
 *     summary: Get unresolved paradoxes
 */
router.get('/paradox/unresolved', async (req, res, next) => {
  try {
    const paradoxes = await tlpParadoxDetectionService.getUnresolvedParadoxes(req.query.portalId as string);
    res.json(paradoxes);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/tlp/buffers:
 *   post:
 *     summary: Create buffer
 */
router.post('/buffers', async (req, res, next) => {
  try {
    const buffer = await tlpBufferService.createBuffer(req.body);
    res.status(201).json(buffer);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/tlp/buffers/:bufferId/allocate:
 *   post:
 *     summary: Allocate buffer
 */
router.post('/buffers/:bufferId/allocate', async (req, res, next) => {
  try {
    const buffer = await tlpBufferService.allocateBuffer({
      bufferId: req.params.bufferId,
      amount: req.body.amount,
    });
    res.json(buffer);
  } catch (error) {
      return next(error);
  }
});

export default router;

