/**
 * @swagger
 * tags:
 *   name: GRU/FX
 *   description: GRU/FX rate discovery, conversion, and history endpoints for Nostro/Vostro
 */

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

const router = Router();

/**
 * @swagger
 * /api/v1/nostro-vostro/gru-fx/rates:
 *   get:
 *     summary: Get real-time GRU/FX rate
 *     description: Retrieve current exchange rate for a currency pair (including GRU pairs)
 *     tags: [GRU/FX]
 *     security:
 *       - SovereignToken: []
 *     parameters:
 *       - in: query
 *         name: pair
 *         required: true
 *         schema:
 *           type: string
 *         description: Currency pair (e.g., "USD/GRU", "GRU/EUR", "USD/EUR")
 *         example: "USD/GRU"
 *     responses:
 *       200:
 *         description: Current exchange rate
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   $ref: '#/components/schemas/GruFxRate'
 *             example:
 *               success: true
 *               data:
 *                 pair: "USD/GRU"
 *                 rate: "0.6667"
 *                 timestamp: "2024-01-15T10:30:00Z"
 *                 source: "DBIS_GRU"
 *                 bid: "0.6663"
 *                 ask: "0.6671"
 *                 spread: "0.0008"
 *       400:
 *         $ref: '#/components/responses/BadRequest'
 *       401:
 *         $ref: '#/components/responses/Unauthorized'
 */
router.get('/rates', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const pair = req.query.pair as string;

    if (!pair) {
      return res.status(400).json({
        success: false,
        error: { code: 'VALIDATION_ERROR', message: 'pair parameter is required' },
        timestamp: new Date(),
      });
    }

    const rate = await gruFxService.getRate(pair);

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

/**
 * @swagger
 * /api/v1/nostro-vostro/gru-fx/rates/history:
 *   get:
 *     summary: Get rate history
 *     description: Retrieve historical exchange rates for a currency pair
 *     tags: [GRU/FX]
 *     security:
 *       - SovereignToken: []
 *     parameters:
 *       - in: query
 *         name: pair
 *         required: true
 *         schema:
 *           type: string
 *         description: Currency pair
 *       - in: query
 *         name: startDate
 *         required: true
 *         schema:
 *           type: string
 *           format: date
 *         description: Start date (ISO 8601)
 *       - in: query
 *         name: endDate
 *         required: true
 *         schema:
 *           type: string
 *           format: date
 *         description: End date (ISO 8601)
 *       - in: query
 *         name: interval
 *         schema:
 *           type: string
 *           enum: [1h, 1d, 1w]
 *           default: 1d
 *         description: Data point interval
 *     responses:
 *       200:
 *         description: Rate history
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   $ref: '#/components/schemas/GruFxRateHistory'
 *       400:
 *         $ref: '#/components/responses/BadRequest'
 */
router.get('/rates/history', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const pair = req.query.pair as string;
    const startDate = req.query.startDate as string;
    const endDate = req.query.endDate as string;
    const interval = req.query.interval as '1h' | '1d' | '1w' | undefined;

    if (!pair || !startDate || !endDate) {
      return res.status(400).json({
        success: false,
        error: {
          code: 'VALIDATION_ERROR',
          message: 'pair, startDate, and endDate parameters are required',
        },
        timestamp: new Date(),
      });
    }

    const history = await gruFxService.getRateHistory(
      pair,
      new Date(startDate),
      new Date(endDate),
      interval
    );

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

/**
 * @swagger
 * /api/v1/nostro-vostro/gru-fx/convert:
 *   post:
 *     summary: Convert currency using GRU/FX rates
 *     description: Convert between currencies using GRU as intermediary or direct rates
 *     tags: [GRU/FX]
 *     security:
 *       - SovereignToken: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/GruFxConversionRequest'
 *           example:
 *             fromCurrency: "USD"
 *             toCurrency: "EUR"
 *             amount: "1000.00"
 *             settlementAsset: "FIAT"
 *             valueDate: "2024-01-15"
 *     responses:
 *       200:
 *         description: Conversion result
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   $ref: '#/components/schemas/GruFxConversionResponse'
 *       400:
 *         $ref: '#/components/responses/BadRequest'
 */
router.post('/convert', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const conversion = await gruFxService.convert(req.body);

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

/**
 * @swagger
 * /api/v1/nostro-vostro/gru-fx/pairs:
 *   get:
 *     summary: Get available currency pairs
 *     description: List all available currency pairs (including GRU pairs)
 *     tags: [GRU/FX]
 *     security:
 *       - SovereignToken: []
 *     responses:
 *       200:
 *         description: List of available pairs
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     pairs:
 *                       type: array
 *                       items:
 *                         type: string
 *                       example: ["USD/GRU", "EUR/GRU", "USD/EUR"]
 */
router.get('/pairs', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const pairs = await gruFxService.getAvailablePairs();

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

export default router;

