// GSDS API Routes

import { Router } from 'express';
import { gsdsContractService } from './gsds-contract.service';
import { gsdsPricingService } from './gsds-pricing.service';
import { gsdsCollateralService } from './gsds-collateral.service';
import { gsdsSettlementService } from './gsds-settlement.service';

const router = Router();

/**
 * @swagger
 * /api/v1/gsds/contract:
 *   post:
 *     summary: Create synthetic derivative contract
 */
router.post('/contract', async (req, res, next) => {
  try {
    const result = await gsdsContractService.createContract(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gsds/contract/:derivativeId:
 *   get:
 *     summary: Get derivative contract
 */
router.get('/contract/:derivativeId', async (req, res, next) => {
  try {
    const contract = await gsdsContractService.getContract(req.params.derivativeId);
    if (!contract) {
      return res.status(404).json({ error: 'Contract not found' });
    }
    res.json(contract);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gsds/contract/:derivativeId/activate:
 *   post:
 *     summary: Activate contract (check collateral and SRI)
 */
router.post('/contract/:derivativeId/activate', async (req, res, next) => {
  try {
    await gsdsContractService.activateContract(req.params.derivativeId);
    res.json({ status: 'activated' });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gsds/contracts:
 *   get:
 *     summary: List derivatives for a bank
 */
router.get('/contracts', async (req, res, next) => {
  try {
    const { bankId, status } = req.query;
    if (!bankId) {
      return res.status(400).json({ error: 'bankId is required' });
    }
    const contracts = await gsdsContractService.listDerivatives(
      bankId as string,
      status as string | undefined
    );
    res.json(contracts);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gsds/pricing:
 *   post:
 *     summary: Calculate synthetic derivative price
 */
router.post('/pricing', async (req, res, next) => {
  try {
    const result = await gsdsPricingService.calculatePrice(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gsds/pricing/:derivativeId:
 *   get:
 *     summary: Get latest pricing for derivative
 */
router.get('/pricing/:derivativeId', async (req, res, next) => {
  try {
    const pricing = await gsdsPricingService.getLatestPricing(req.params.derivativeId);
    if (!pricing) {
      return res.status(404).json({ error: 'Pricing not found' });
    }
    res.json(pricing);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gsds/collateral:
 *   post:
 *     summary: Add collateral to derivative
 */
router.post('/collateral', async (req, res, next) => {
  try {
    const result = await gsdsCollateralService.addCollateral(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gsds/collateral/:collateralId/release:
 *   post:
 *     summary: Release collateral
 */
router.post('/collateral/:collateralId/release', async (req, res, next) => {
  try {
    await gsdsCollateralService.releaseCollateral(req.params.collateralId);
    res.json({ status: 'released' });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gsds/collateral/:derivativeId:
 *   get:
 *     summary: Get collateral for derivative
 */
router.get('/collateral/:derivativeId', async (req, res, next) => {
  try {
    const collaterals = await gsdsCollateralService.getCollateral(req.params.derivativeId);
    res.json(collaterals);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gsds/settlement:
 *   post:
 *     summary: Execute settlement
 */
router.post('/settlement', async (req, res, next) => {
  try {
    const result = await gsdsSettlementService.executeSettlement(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gsds/settlement/:settlementId/finalize:
 *   post:
 *     summary: Finalize settlement
 */
router.post('/settlement/:settlementId/finalize', async (req, res, next) => {
  try {
    await gsdsSettlementService.finalizeSettlement(req.params.settlementId);
    res.json({ status: 'finalized' });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gsds/settlement/:settlementId:
 *   get:
 *     summary: Get settlement
 */
router.get('/settlement/:settlementId', async (req, res, next) => {
  try {
    const settlement = await gsdsSettlementService.getSettlement(req.params.settlementId);
    if (!settlement) {
      return res.status(404).json({ error: 'Settlement not found' });
    }
    res.json(settlement);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gsds/settlements/:derivativeId:
 *   get:
 *     summary: List settlements for derivative
 */
router.get('/settlements/:derivativeId', async (req, res, next) => {
  try {
    const settlements = await gsdsSettlementService.listSettlements(req.params.derivativeId);
    res.json(settlements);
  } catch (error) {
      return next(error);
  }
});

export default router;

