/**
 * Feature Flags Configuration
 * Mengontrol aktivasi fitur Sprint 2 dan fitur lainnya
 */

export interface FeatureFlags {
  // Sprint 2 Features
  enhancedQuizInterface: boolean;
  progressIndicator: boolean;
  enhancedTimer: boolean;
  enhancedQuestionNavigator: boolean;
  quizIntroductionModal: boolean;
  confidenceRating: boolean;
  autoSaveIndicator: boolean;
  answerFeedback: boolean;
  bookmarkFeature: boolean;
  searchAndFilter: boolean;
  
  // UI Enhancements
  modernDesign: boolean;
  animations: boolean;
  responsiveLayout: boolean;
  
  // Performance Features
  autoSave: boolean;
  offlineSupport: boolean;
  
  // Analytics
  userBehaviorTracking: boolean;
  performanceMetrics: boolean;
}

// Default feature flags - dapat diubah berdasarkan environment
const defaultFlags: FeatureFlags = {
  // Sprint 2 Features - Aktif secara default
  enhancedQuizInterface: true,
  progressIndicator: true,
  enhancedTimer: true,
  enhancedQuestionNavigator: true,
  quizIntroductionModal: true,
  confidenceRating: true,
  autoSaveIndicator: true,
  answerFeedback: true,
  bookmarkFeature: true,
  searchAndFilter: true,
  
  // UI Enhancements
  modernDesign: true,
  animations: true,
  responsiveLayout: true,
  
  // Performance Features
  autoSave: true,
  offlineSupport: false, // Belum diimplementasi
  
  // Analytics
  userBehaviorTracking: false, // Opsional
  performanceMetrics: false, // Opsional
};

// Environment-specific overrides
const environmentFlags: Partial<Record<string, Partial<FeatureFlags>>> = {
  development: {
    // Semua fitur aktif untuk development
    ...defaultFlags,
    userBehaviorTracking: true,
    performanceMetrics: true,
  },
  
  staging: {
    // Testing environment - semua fitur Sprint 2 aktif
    ...defaultFlags,
    offlineSupport: false,
  },
  
  production: {
    // Production - fitur stabil saja
    enhancedQuizInterface: true,
    progressIndicator: true,
    enhancedTimer: true,
    enhancedQuestionNavigator: true,
    quizIntroductionModal: true,
    confidenceRating: true,
    autoSaveIndicator: true,
    answerFeedback: true,
    bookmarkFeature: true,
    searchAndFilter: true,
    modernDesign: true,
    animations: true,
    responsiveLayout: true,
    autoSave: true,
    offlineSupport: false,
    userBehaviorTracking: false,
    performanceMetrics: false,
  },
};

// Get current environment
const getCurrentEnvironment = (): string => {
  if (typeof window === 'undefined') {
    return process.env.NODE_ENV || 'development';
  }
  
  const hostname = window.location.hostname;
  
  if (hostname === 'localhost' || hostname === '127.0.0.1') {
    return 'development';
  } else if (hostname.includes('staging') || hostname.includes('test')) {
    return 'staging';
  } else {
    return 'production';
  }
};

// Get feature flags based on environment
export const getFeatureFlags = (): FeatureFlags => {
  const environment = getCurrentEnvironment();
  const envFlags = environmentFlags[environment] || {};
  
  return {
    ...defaultFlags,
    ...envFlags,
  };
};

// Individual feature flag checkers
export const isFeatureEnabled = (feature: keyof FeatureFlags): boolean => {
  const flags = getFeatureFlags();
  return flags[feature];
};

// Sprint 2 specific checker
export const isSprint2Enabled = (): boolean => {
  const flags = getFeatureFlags();
  return flags.enhancedQuizInterface;
};

// Bulk feature checker
export const areAllSprint2FeaturesEnabled = (): boolean => {
  const flags = getFeatureFlags();
  const sprint2Features: (keyof FeatureFlags)[] = [
    'enhancedQuizInterface',
    'progressIndicator',
    'enhancedTimer',
    'enhancedQuestionNavigator',
    'quizIntroductionModal',
    'confidenceRating',
    'autoSaveIndicator',
    'answerFeedback',
    'bookmarkFeature',
    'searchAndFilter',
  ];
  
  return sprint2Features.every(feature => flags[feature]);
};

// Feature flag context for React components
export const createFeatureFlagContext = () => {
  const flags = getFeatureFlags();
  
  return {
    flags,
    isEnabled: (feature: keyof FeatureFlags) => flags[feature],
    isSprint2Enabled: () => flags.enhancedQuizInterface,
    areAllSprint2FeaturesEnabled: () => areAllSprint2FeaturesEnabled(),
  };
};

// Export flags for direct usage
export const featureFlags = getFeatureFlags();

// Development helper - log active features
if (getCurrentEnvironment() === 'development') {
  console.log('🚀 Active Feature Flags:', getFeatureFlags());
  console.log('📊 Sprint 2 Status:', isSprint2Enabled() ? '✅ Enabled' : '❌ Disabled');
}