// FX Service Edge Cases Unit Tests

import { fxService } from '@/core/fx/fx.service';
import { createTestSovereignBank, cleanDatabase } from '@/__tests__/utils/test-db';
import { testPrisma } from '@/__tests__/utils/test-db';
import { FxOrderType, FxTradeType } from '@/shared/types';

describe('FxService - Edge Cases', () => {
  beforeEach(async () => {
    await cleanDatabase();
  });

  afterAll(async () => {
    await testPrisma.$disconnect();
  });

  describe('Market Volatility', () => {
    it('should handle rapid price changes', async () => {
      const bank = await createTestSovereignBank();

      const order1 = await fxService.submitOrder(bank.id, {
        pair: 'USD/EUR',
        amount: '1000.00',
        orderType: FxOrderType.MARKET,
        settlement: 'RTGS',
      });

      // Simulate rapid price change scenario
      const order2 = await fxService.submitOrder(bank.id, {
        pair: 'USD/EUR',
        amount: '1000.00',
        orderType: FxOrderType.MARKET,
        settlement: 'RTGS',
      });

      expect(order1.tradeId).toBeDefined();
      expect(order2.tradeId).toBeDefined();
    });
  });

  describe('Order Matching', () => {
    it('should match limit orders at correct price', async () => {
      const bank = await createTestSovereignBank();

      const order = await fxService.submitOrder(bank.id, {
        pair: 'USD/EUR',
        amount: '1000.00',
        orderType: FxOrderType.LIMIT,
        limitPrice: '0.85',
        settlement: 'RTGS',
      });

      expect(order.tradeId).toBeDefined();
      expect(order.status).toBe('pending');
    });

    it('should not match limit orders below market price', async () => {
      const bank = await createTestSovereignBank();

      // This test would need market price mocking
      const order = await fxService.submitOrder(bank.id, {
        pair: 'USD/EUR',
        amount: '1000.00',
        orderType: FxOrderType.LIMIT,
        limitPrice: '0.50', // Unrealistically low
        settlement: 'RTGS',
      });

      expect(order.status).toBe('pending');
    });
  });

  describe('Settlement Failures', () => {
    it('should handle settlement failure gracefully', async () => {
      const bank = await createTestSovereignBank();

      const order = await fxService.submitOrder(bank.id, {
        pair: 'USD/EUR',
        amount: '1000.00',
        orderType: FxOrderType.MARKET,
        settlement: 'RTGS',
      });

      // Attempt to execute
      try {
        await fxService.executeTrade(order.tradeId);
      } catch (error) {
        // Settlement failure should be handled
        expect(error).toBeDefined();
      }
    });
  });

  describe('Invalid Currency Pairs', () => {
    it('should reject invalid currency pair format', async () => {
      const bank = await createTestSovereignBank();

      await expect(
        fxService.submitOrder(bank.id, {
          pair: 'INVALID',
          amount: '1000.00',
          orderType: FxOrderType.MARKET,
          settlement: 'RTGS',
        })
      ).rejects.toThrow();
    });
  });

  describe('Large Order Amounts', () => {
    it('should handle very large FX orders', async () => {
      const bank = await createTestSovereignBank();

      const order = await fxService.submitOrder(bank.id, {
        pair: 'USD/EUR',
        amount: '999999999.99',
        orderType: FxOrderType.MARKET,
        settlement: 'RTGS',
      });

      expect(order.tradeId).toBeDefined();
    });
  });
});

