// CBDS API Routes

import { Router } from 'express';
import { cdtService } from './cdt-service';
import { reserveCertificateService } from './reserve-certificate.service';
import { cdtSettlementService } from './cdt-settlement.service';

const router = Router();

/**
 * @swagger
 * /api/v1/cbds/cdt/mint:
 *   post:
 *     summary: Mint CDT from reserves
 */
router.post('/cdt/mint', async (req, res, next) => {
  try {
    const cdtId = await cdtService.mintCdt(req.body);
    res.json({ cdtId });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/cbds/cdt/burn:
 *   post:
 *     summary: Burn CDT
 */
router.post('/cdt/burn', async (req, res, next) => {
  try {
    const { cdtId, reason } = req.body;
    const result = await cdtService.burnCdt(cdtId, reason);
    res.json({ success: result });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/cbds/cdt/:cdtId:
 *   get:
 *     summary: Get CDT details
 */
router.get('/cdt/:cdtId', async (req, res, next) => {
  try {
    const cdt = await cdtService.getCdt(req.params.cdtId);
    if (!cdt) {
      return res.status(404).json({ error: 'CDT not found' });
    }
    res.json(cdt);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/cbds/reserve-certificate/create:
 *   post:
 *     summary: Create reserve certificate
 */
router.post('/reserve-certificate/create', async (req, res, next) => {
  try {
    const certificateId = await reserveCertificateService.createReserveCertificate(req.body);
    res.json({ certificateId });
  } catch (error) {
      return next(error);
  }
});

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

export default router;

