// ALPS API Routes

import { Router } from 'express';
import { alpsService } from './alps.service';
import { liquidityMonitorService } from './liquidity-monitor.service';
import { stressPredictorService } from './stress-predictor.service';
import { liquidityExecutorService } from './liquidity-executor.service';

const router = Router();

/**
 * @swagger
 * /api/v1/alps/run:
 *   post:
 *     summary: Run autonomous liquidity engine
 *     tags: [ALPS]
 */
router.post('/run', async (req, res, next) => {
  try {
    await alpsService.runAutonomousEngine();
    res.json({ message: 'ALPS engine executed successfully' });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/alps/monitor:
 *   get:
 *     summary: Monitor liquidity for all sovereigns
 *     tags: [ALPS]
 */
router.get('/monitor', async (req, res, next) => {
  try {
    const ratios = await alpsService.monitorLiquidity();
    res.json(ratios);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/alps/monitor/:sovereignBankId:
 *   get:
 *     summary: Get SXLR for sovereign
 *     tags: [ALPS]
 */
router.get('/monitor/:sovereignBankId', async (req, res, next) => {
  try {
    const sxlr = await liquidityMonitorService.getSXLR(req.params.sovereignBankId);
    if (!sxlr) {
      return res.status(404).json({ error: 'SXLR not found' });
    }
    res.json(sxlr);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/alps/stress/predict:
 *   post:
 *     summary: Predict liquidity stress events
 *     tags: [ALPS]
 */
router.post('/stress/predict', async (req, res, next) => {
  try {
    const events = await alpsService.predictStressEvents();
    res.json(events);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/alps/stress:
 *   get:
 *     summary: Get predicted stress events
 *     tags: [ALPS]
 */
router.get('/stress', async (req, res, next) => {
  try {
    const events = await stressPredictorService.getPredictedStressEvents(
      req.query.sovereignBankId as string
    );
    res.json(events);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/alps/actions:
 *   get:
 *     summary: Get liquidity actions
 *     tags: [ALPS]
 */
router.get('/actions', async (req, res, next) => {
  try {
    const actions = await alpsService.getActions(
      req.query.sovereignBankId as string,
      req.query.actionType as any
    );
    res.json(actions);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/alps/inject:
 *   post:
 *     summary: Execute liquidity injection
 *     tags: [ALPS]
 */
router.post('/inject', async (req, res, next) => {
  try {
    const action = await liquidityExecutorService.executeInjection(
      req.body.sovereignBankId,
      req.body.amount,
      req.body.triggerReason || 'Manual injection'
    );
    res.json(action);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/alps/withdraw:
 *   post:
 *     summary: Execute liquidity withdrawal
 *     tags: [ALPS]
 */
router.post('/withdraw', async (req, res, next) => {
  try {
    const action = await liquidityExecutorService.executeWithdrawal(
      req.body.sovereignBankId,
      req.body.amount,
      req.body.triggerReason || 'Manual withdrawal'
    );
    res.json(action);
  } catch (error) {
      return next(error);
  }
});

export default router;

