// Ledger API Integration Tests

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

describe('Ledger API Integration', () => {
  beforeEach(async () => {
    await cleanDatabase();
  });

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

  describe('POST /api/ledger/entries', () => {
    it('should create a ledger entry', async () => {
      const bank = await createTestSovereignBank();
      const debitAccount = await createTestBankAccount({
        sovereignBankId: bank.id,
        balance: '1000.00',
      });
      const creditAccount = await createTestBankAccount({
        sovereignBankId: bank.id,
      });

      const response = await request(app)
        .post('/api/ledger/entries')
        .set(createAuthHeaders({ sovereignBankId: bank.id }))
        .send({
          ledgerId: 'MASTER',
          debitAccountId: debitAccount.id,
          creditAccountId: creditAccount.id,
          amount: '100.00',
          currencyCode: 'USD',
          assetType: 'fiat',
          transactionType: 'TYPE_A',
          referenceId: 'TEST-REF-001',
        });

      expect([200, 201]).toContain(response.status);
      expect(response.body.success).toBe(true);
    });
  });

  describe('GET /api/ledger/entries/:id', () => {
    it('should retrieve a ledger entry', async () => {
      const bank = await createTestSovereignBank();

      // Note: This test assumes the route exists
      // Adjust based on actual API structure
      const response = await request(app)
        .get('/api/ledger/entries/test-id')
        .set(createAuthHeaders({ sovereignBankId: bank.id }));

      expect([200, 404]).toContain(response.status);
    });
  });
});

