/**
 * @swagger
 * tags:
 *   name: FX
 *   description: Foreign Exchange Trading and Settlement
 */

import { Router } from 'express';
import { zeroTrustAuthMiddleware } from '@/integration/api-gateway/middleware/auth.middleware';
import { fxService } from './fx.service';

const router = Router();

/**
 * @swagger
 * /api/fx/orders:
 *   post:
 *     summary: Submit an FX order
 *     description: Create a new FX trade order (market or limit)
 *     tags: [FX]
 *     security:
 *       - SovereignToken: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - pair
 *               - amount
 *               - orderType
 *               - settlement
 *             properties:
 *               pair:
 *                 type: string
 *                 description: Currency pair (e.g., USD/EUR)
 *                 example: "USD/EUR"
 *               amount:
 *                 type: string
 *                 description: Order amount
 *                 example: "1000.00"
 *               orderType:
 *                 type: string
 *                 enum: [market, limit, stop, sovereign]
 *               limitPrice:
 *                 type: string
 *                 description: Limit price (required for limit orders)
 *               settlement:
 *                 type: string
 *                 enum: [RTGS, T+1, atomic]
 *     responses:
 *       201:
 *         description: Order submitted successfully
 *       400:
 *         description: Validation error
 */
router.post('/orders', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const sovereignBankId = (req as any).sovereignBankId;
    const result = await fxService.submitOrder(sovereignBankId, {
      pair: req.body.pair,
      amount: req.body.amount,
      orderType: req.body.orderType,
      limitPrice: req.body.limitPrice,
      settlement: req.body.settlement,
    });

    res.status(201).json({
      success: true,
      data: result,
      timestamp: new Date(),
    });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/fx/trades/{id}:
 *   get:
 *     summary: Get FX trade by ID
 *     description: Retrieve FX trade details
 *     tags: [FX]
 *     security:
 *       - SovereignToken: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Trade retrieved
 *       404:
 *         description: Trade not found
 */
router.get('/trades/:id', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const trade = await fxService.executeTrade(req.params.id);
    res.json({
      success: true,
      data: trade,
      timestamp: new Date(),
    });
  } catch (error) {
    if ((error as Error).message.includes('not found')) {
      return res.status(404).json({
        success: false,
        error: { code: 'NOT_FOUND', message: 'FX trade not found' },
        timestamp: new Date(),
      });
    }
    next(error);
  }
});

export default router;

