/**
 * Entra VerifiedID Integration Tests
 * These tests require actual Entra VerifiedID configuration
 * Set ENTRA_TEST_* environment variables to run
 */

import { describe, it, expect, beforeAll } from 'vitest';
import { EnhancedEntraVerifiedIDClient } from './entra-verifiedid-enhanced';
import { getEnv } from '@the-order/shared';

describe('Entra VerifiedID Integration Tests', () => {
  let client: EnhancedEntraVerifiedIDClient | null = null;

  beforeAll(() => {
    const env = getEnv();
    
    if (
      !env.ENTRA_TENANT_ID ||
      !env.ENTRA_CLIENT_ID ||
      !env.ENTRA_CLIENT_SECRET ||
      !env.ENTRA_CREDENTIAL_MANIFEST_ID
    ) {
      console.warn('Entra VerifiedID credentials not configured, skipping integration tests');
      return;
    }

    client = new EnhancedEntraVerifiedIDClient({
      tenantId: env.ENTRA_TENANT_ID,
      clientId: env.ENTRA_CLIENT_ID,
      clientSecret: env.ENTRA_CLIENT_SECRET,
      credentialManifestId: env.ENTRA_CREDENTIAL_MANIFEST_ID,
    });
  });

  it('should issue a credential', async () => {
    if (!client) {
      return; // Skip if not configured
    }

    const request = {
      claims: {
        email: 'test@example.com',
        name: 'Test User',
        test: 'true',
      },
    };

    const response = await client.issueCredential(request);
    
    expect(response).toBeDefined();
    expect(response.requestId).toBeDefined();
    expect(response.url).toBeDefined();
    expect(response.expiry).toBeGreaterThan(0);
  }, 30000); // 30 second timeout

  it('should check issuance status', async () => {
    if (!client) {
      return; // Skip if not configured
    }

    // First issue a credential
    const issueResponse = await client.issueCredential({
      claims: { email: 'test@example.com' },
    });

    // Then check status
    const status = await client.getIssuanceStatus(issueResponse.requestId);
    
    expect(status).toBeDefined();
    expect(status.requestId).toBe(issueResponse.requestId);
    expect(['request_created', 'request_retrieved', 'issuance_successful', 'issuance_failed']).toContain(status.state);
  }, 30000);

  it('should support multi-manifest', async () => {
    if (!client) {
      return; // Skip if not configured
    }

    // Register additional manifest
    client.registerManifest('test', 'test-manifest-id');

    const manifests = client.getManifests();
    expect(manifests.test).toBe('test-manifest-id');
  });

  it('should handle retries on transient errors', async () => {
    if (!client) {
      return; // Skip if not configured
    }

    // This test would require mocking or simulating transient errors
    // For now, we just verify retry config is set
    expect(client).toBeDefined();
  });
});

