// FX Trading Flow End-to-End Tests

import request from 'supertest';
import app from '@/integration/api-gateway/app';
import { createTestSovereignBank, cleanDatabase } from '@/__tests__/utils/test-db';
import { testPrisma } from '@/__tests__/utils/test-db';
import { createAuthHeaders } from '@/__tests__/utils/test-auth';

describe('FX Trading Flow E2E', () => {
  beforeEach(async () => {
    await cleanDatabase();
  });

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

  it('should complete full FX trading workflow', async () => {
    const bank = await createTestSovereignBank();

    // Step 1: Submit FX order
    const orderResponse = await request(app)
      .post('/api/fx/orders')
      .set(createAuthHeaders({ sovereignBankId: bank.id }))
      .send({
        pair: 'USD/EUR',
        amount: '1000.00',
        orderType: 'market',
        settlement: 'RTGS',
      });

    expect([200, 201]).toContain(orderResponse.status);
    const tradeId = orderResponse.body.data?.tradeId;

    if (tradeId) {
      // Step 2: Execute trade
      const executeResponse = await request(app)
        .post(`/api/fx/trades/${tradeId}/execute`)
        .set(createAuthHeaders({ sovereignBankId: bank.id }));

      expect([200, 201]).toContain(executeResponse.status);

      // Step 3: Check trade status
      const statusResponse = await request(app)
        .get(`/api/fx/trades/${tradeId}`)
        .set(createAuthHeaders({ sovereignBankId: bank.id }));

      expect(statusResponse.status).toBe(200);
      expect(['executed', 'settled']).toContain(statusResponse.body.data?.status);
    }
  });
});

