// Singularity-Grade Liquidity Engine API Routes

import { Router } from 'express';
import { sgleEngineService } from './sgle-engine.service';
import { sgleContinuumService } from './sgle-continuum.service';
import { sgleGenerationService } from './sgle-generation.service';

const router = Router();

/**
 * @swagger
 * /api/v1/sgle/project:
 *   post:
 *     summary: Project future liquidity
 */
router.post('/project', async (req, res, next) => {
  try {
    const projection = await sgleEngineService.projectFutureLiquidity(req.body);
    res.status(201).json(projection);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/sgle/projection/:projectionId:
 *   get:
 *     summary: Get projection by ID
 */
router.get('/projection/:projectionId', async (req, res, next) => {
  try {
    const projection = await sgleEngineService.getProjection(req.params.projectionId);
    if (!projection) {
      return res.status(404).json({ error: 'Projection not found' });
    }
    res.json(projection);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/sgle/projections/gaps:
 *   get:
 *     summary: Get projections with liquidity gaps
 */
router.get('/projections/gaps', async (req, res, next) => {
  try {
    const projections = await sgleEngineService.getProjectionsWithGaps();
    res.json(projections);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/sgle/continuum/state:
 *   get:
 *     summary: Get continuum state
 */
router.get('/continuum/state', async (req, res, next) => {
  try {
    const state = await sgleContinuumService.getContinuumState();
    res.json(state);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/sgle/continuum/stability:
 *   get:
 *     summary: Check continuum stability
 */
router.get('/continuum/stability', async (req, res, next) => {
  try {
    const stability = await sgleContinuumService.checkContinuumStability();
    res.json(stability);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/sgle/generate/:gapId:
 *   post:
 *     summary: Auto-generate liquidity for gap
 */
router.post('/generate/:gapId', async (req, res, next) => {
  try {
    const { conservationLimit } = req.body;
    const result = await sgleGenerationService.autoGenerateLiquidity(
      req.params.gapId,
      conservationLimit
    );
    res.status(201).json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/sgle/generate/all:
 *   post:
 *     summary: Detect and generate liquidity for all gaps
 */
router.post('/generate/all', async (req, res, next) => {
  try {
    const { conservationLimit } = req.body;
    const results = await sgleGenerationService.detectAndGenerateLiquidity(conservationLimit);
    res.json(results);
  } catch (error) {
      return next(error);
  }
});

export default router;

