// GAS API Routes

import { Router } from 'express';
import { gasSettlementService } from './gas-settlement.service';
import { gasRoutingService } from './gas-routing.service';
import { gasCommitmentService } from './gas-commitment.service';
import { zeroTrustAuthMiddleware } from '@/integration/api-gateway/middleware/auth.middleware';

const router = Router();

/**
 * @swagger
 * tags:
 *   name: GAS Settlement
 *   description: Global Atomic Settlements (GAS) Network
 */

/**
 * @swagger
 * /api/gas/settle:
 *   post:
 *     summary: Execute GAS atomic settlement
 *     description: Execute multi-dimensional atomic settlement through GAS network
 *     tags: [GAS Settlement]
 *     security:
 *       - SovereignToken: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - sourceBankId
 *               - destinationBankId
 *               - amount
 *             properties:
 *               sourceBankId:
 *                 type: string
 *               destinationBankId:
 *                 type: string
 *               amount:
 *                 type: string
 *     responses:
 *       200:
 *         description: Settlement executed successfully
 */
router.post(
  '/settle',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await gasSettlementService.executeGasSettlement(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/gas/settlement/:gasSettlementId
 * @desc Get GAS settlement by ID
 */
router.get(
  '/settlement/:gasSettlementId',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const settlement = await gasSettlementService.getGasSettlement(
        req.params.gasSettlementId
      );
      if (!settlement) {
        return res.status(404).json({ error: 'Settlement not found' });
      }
      res.json(settlement);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/gas/settlements/bank/:sovereignBankId
 * @desc Get GAS settlements for a bank
 */
router.get(
  '/settlements/bank/:sovereignBankId',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const limit = parseInt(req.query.limit as string) || 100;
      const settlements =
        await gasSettlementService.getGasSettlementsForBank(
          req.params.sovereignBankId,
          limit
        );
      res.json(settlements);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/gas/settlements/network/:networkType
 * @desc Get GAS settlements by network type
 */
router.get(
  '/settlements/network/:networkType',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const limit = parseInt(req.query.limit as string) || 100;
      const settlements =
        await gasSettlementService.getGasSettlementsByNetworkType(
          req.params.networkType,
          limit
        );
      res.json(settlements);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/gas/route
 * @desc Calculate optimal route
 */
router.post(
  '/route',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await gasRoutingService.calculateOptimalRoute(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/gas/routing/history
 * @desc Get routing history
 */
router.get(
  '/routing/history',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const sourceBankId = req.query.sourceBankId as string | undefined;
      const destinationBankId = req.query.destinationBankId as string | undefined;
      const limit = parseInt(req.query.limit as string) || 100;
      const history = await gasRoutingService.getRoutingHistory(
        sourceBankId,
        destinationBankId,
        limit
      );
      res.json(history);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/gas/commitment/:settlementId
 * @desc Get commitment by settlement ID
 */
router.get(
  '/commitment/:settlementId',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const commitment = await gasCommitmentService.getCommitment(
        req.params.settlementId
      );
      if (!commitment) {
        return res.status(404).json({ error: 'Commitment not found' });
      }
      res.json(commitment);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/gas/commitment/verify
 * @desc Verify all commits match
 */
router.post(
  '/commitment/verify',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const { settlementId } = req.body;
      const result = await gasCommitmentService.verifyAllCommitsMatch(
        settlementId
      );
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

export default router;

