/**
 * Integration test to verify the feature flag system works end-to-end
 */

import { describe, expect, it } from 'vitest';
import { getFeatureState, isFeatureEnabled, loadFeatureConfig } from './feature-loader';

describe('Feature Flag Integration Test', () => {
  it('should load config and provide correct feature states', async () => {
    // Set test environment
    process.env.NODE_ENV = 'test';
    
    const result = await loadFeatureConfig();
    
    // Verify config loaded from file
    expect(result.loadedFrom).toBe('config-file');
    expect(result.configPath).toBe('apps/api/src/config/features.config.ts');
    
    // Verify core platform features (new structure)
    expect(result.features['platform.dashboard']).toBe('enabled');
    expect(result.features['platform.leagues']).toBe('enabled');
    expect(result.features['platform.teams']).toBe('enabled');
    
    // Verify auth features
    expect(result.features['auth.signup']).toBe('enabled');
    expect(result.features['auth.login']).toBe('enabled');
    
    // Verify sponsor features (expanded)
    expect(result.features['sponsors.portal']).toBe('enabled');
    expect(result.features['sponsors.dashboard']).toBe('enabled');
    expect(result.features['sponsors.campaigns']).toBe('enabled');
    
    // Verify admin features
    expect(result.features['admin.dashboard']).toBe('enabled');
    expect(result.features['admin.userManagement']).toBe('enabled');
    
    // Verify beta features (controlled in test)
    expect(result.features['beta.newUI']).toBe('disabled');
    expect(result.features['beta.experimental']).toBe('disabled');
    
    // Verify utility functions work
    expect(isFeatureEnabled(result.features, 'platform.dashboard')).toBe(true);
    expect(isFeatureEnabled(result.features, 'beta.newUI')).toBe(false);
    expect(getFeatureState(result.features, 'platform.dashboard')).toBe('enabled');
    expect(getFeatureState(result.features, 'nonexistent')).toBe('disabled');
  });

  it('should work with different environments', async () => {
    // Test development environment - alpha mode (all enabled)
    process.env.NODE_ENV = 'development';
    const devResult = await loadFeatureConfig();
    expect(devResult.features['beta.newUI']).toBe('enabled');
    expect(devResult.features['platform.dashboard']).toBe('enabled');
    expect(devResult.features['sponsors.management']).toBe('enabled');
    
    // Test production environment - beta mode (controlled rollout)
    process.env.NODE_ENV = 'production';
    const prodResult = await loadFeatureConfig();
    expect(prodResult.features['beta.newUI']).toBe('disabled');
    expect(prodResult.features['platform.dashboard']).toBe('enabled');
    expect(prodResult.features['sponsors.management']).toBe('disabled'); // Not ready yet
    expect(prodResult.features['admin.analytics']).toBe('disabled'); // Not ready yet
  });
  
  it('should handle the new two-tier architecture correctly', async () => {
    // Development should have all platform features enabled (alpha mode)
    process.env.NODE_ENV = 'development';
    const devResult = await loadFeatureConfig();
    
    // All core platform features should be enabled
    expect(devResult.features['platform.dashboard']).toBe('enabled');
    expect(devResult.features['platform.leagues']).toBe('enabled');
    expect(devResult.features['platform.teams']).toBe('enabled');
    expect(devResult.features['platform.drivers']).toBe('enabled');
    expect(devResult.features['platform.races']).toBe('enabled');
    expect(devResult.features['platform.leaderboards']).toBe('enabled');
    
    // All auth features should be enabled
    expect(devResult.features['auth.signup']).toBe('enabled');
    expect(devResult.features['auth.login']).toBe('enabled');
    expect(devResult.features['auth.forgotPassword']).toBe('enabled');
    expect(devResult.features['auth.resetPassword']).toBe('enabled');
    
    // Onboarding should be enabled
    expect(devResult.features['onboarding.wizard']).toBe('enabled');
  });

  it('should return flattened features with expected structure', async () => {
    process.env.NODE_ENV = 'test';
    const result = await loadFeatureConfig();
    
    // Verify the result has the expected shape for API response
    expect(result).toHaveProperty('features');
    expect(result).toHaveProperty('loadedFrom');
    expect(result).toHaveProperty('configPath');
    
    // Verify features is a flat object with dot-notation keys
    expect(typeof result.features).toBe('object');
    expect(result.features['platform.dashboard']).toBeDefined();
    expect(result.features['sponsors.portal']).toBeDefined();
  });
});