// SSU API Routes

import { Router } from 'express';
import { ssuService } from './ssu-service';
import { ssuCompositionService } from './ssu-composition.service';
import { ssuTransactionService } from './ssu-transaction.service';

const router = Router();

/**
 * @swagger
 * /api/v1/ssu/mint:
 *   post:
 *     summary: Mint Synthetic Settlement Unit (SSU)
 *     description: Create new SSU tokens with composition (40% currency, 30% commodity, 20% CBDC, 10% LAM)
 *     tags: [Settlement, SSU]
 *     security:
 *       - SovereignToken: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - amount
 *               - sovereignBankId
 *             properties:
 *               amount:
 *                 type: string
 *                 example: "1000.00"
 *               sovereignBankId:
 *                 type: string
 *     responses:
 *       200:
 *         description: SSU minted successfully
 *       400:
 *         description: Validation error
 */
router.post('/mint', async (req, res, next) => {
  try {
    const transactionId = await ssuService.mintSsu(req.body);
    res.json({ transactionId });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/ssu/burn:
 *   post:
 *     summary: Burn Synthetic Settlement Unit (SSU)
 *     description: Destroy SSU tokens and return underlying assets
 *     tags: [Settlement, SSU]
 *     security:
 *       - SovereignToken: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - ssuId
 *               - amount
 *             properties:
 *               ssuId:
 *                 type: string
 *               amount:
 *                 type: string
 *     responses:
 *       200:
 *         description: SSU burned successfully
 */
router.post('/burn', async (req, res, next) => {
  try {
    const transactionId = await ssuService.burnSsu(req.body);
    res.json({ transactionId });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/ssu/composition:
 *   get:
 *     summary: Get SSU composition
 */
router.get('/composition', async (req, res, next) => {
  try {
    const { ssuId } = req.query;
    if (!ssuId) {
      return res.status(400).json({ error: 'ssuId is required' });
    }
    const composition = await ssuCompositionService.getLatestComposition(ssuId as string);
    res.json(composition);
  } catch (error) {
      return next(error);
  }
});

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

/**
 * @swagger
 * /api/v1/ssu/redeem:
 *   post:
 *     summary: Redeem SSU
 */
router.post('/redeem', async (req, res, next) => {
  try {
    const requestId = await ssuService.redeemSsu(req.body);
    res.json({ requestId });
  } catch (error) {
      return next(error);
  }
});

export default router;

