// GSS API Routes

import { Router } from 'express';
import { gssArchitectureService } from './gss-architecture.service';
import { gssMasterLedgerService } from './gss-master-ledger.service';
import { stateBlockService } from './state-block.service';

const router = Router();

/**
 * @swagger
 * /api/v1/gss/settlement/execute:
 *   post:
 *     summary: Execute GSS settlement
 *     description: Execute settlement through Global Settlement System (GSS) four-layer architecture
 *     tags: [Settlement, GSS]
 *     security:
 *       - SovereignToken: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - nodeId
 *               - entries
 *             properties:
 *               nodeId:
 *                 type: string
 *                 description: Settlement node ID
 *               entries:
 *                 type: array
 *                 description: Ledger entries to post
 *     responses:
 *       200:
 *         description: Settlement executed successfully
 *       400:
 *         description: Validation error
 */
router.post('/settlement/execute', async (req, res, next) => {
  try {
    const result = await gssMasterLedgerService.postToMasterLedger(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gss/master-ledger/entries:
 *   get:
 *     summary: Query master ledger entries
 *     description: Query entries from DBIS Master Ledger
 *     tags: [Settlement, GSS]
 *     security:
 *       - SovereignToken: []
 *     parameters:
 *       - in: query
 *         name: nodeId
 *         schema:
 *           type: string
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 100
 *     responses:
 *       200:
 *         description: Master ledger entries retrieved
 */
router.get('/master-ledger/entries', async (req, res, next) => {
  try {
    const { nodeId, status, limit } = req.query;
    const entries = await gssMasterLedgerService.getMasterLedgerEntries(
      nodeId as string,
      status as string,
      parseInt(limit as string) || 100
    );
    res.json(entries);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gss/state-blocks/create:
 *   post:
 *     summary: Create state block
 */
router.post('/state-blocks/create', async (req, res, next) => {
  try {
    const result = await stateBlockService.createStateBlock(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/gss/layers/:layerId:
 *   get:
 *     summary: Get layer status
 */
router.get('/layers/:layerId', async (req, res, next) => {
  try {
    const status = await gssArchitectureService.getLayerStatus(req.params.layerId);
    if (!status) {
      return res.status(404).json({ error: 'Layer not found' });
    }
    res.json(status);
  } catch (error) {
      return next(error);
  }
});

export default router;

