/**
 * TypedFetch Configuration Types
 * Zero-config by default, but fully customizable when needed
 */

export interface TypedFetchConfig {
  /**
   * Cache configuration
   */
  cache?: {
    /** Maximum number of cached entries (default: 500) */
    maxSize?: number
    /** Time to live in milliseconds (default: 300000 - 5 minutes) */
    ttl?: number
    /** Enable/disable caching (default: true) */
    enabled?: boolean
  }
  
  /**
   * Retry configuration
   */
  retry?: {
    /** Maximum retry attempts for failed requests (default: 3 for GET, 1 for others) */
    maxAttempts?: number
    /** Delay between retries in ms (default: [100, 250, 500, 1000, 2000]) */
    delays?: number[]
    /** Retry on these status codes (default: [408, 429, 500, 502, 503, 504]) */
    retryableStatuses?: number[]
  }
  
  /**
   * Circuit breaker configuration
   */
  circuit?: {
    /** Failure threshold before opening circuit (default: 5) */
    threshold?: number
    /** Time before attempting to close circuit in ms (default: 30000) */
    timeout?: number
    /** Enable/disable circuit breaker (default: true) */
    enabled?: boolean
  }
  
  /**
   * Request configuration
   */
  request?: {
    /** Default timeout for requests in ms (default: 30000) */
    timeout?: number
    /** Default headers for all requests */
    headers?: Record<string, string>
    /** Base URL for all requests */
    baseURL?: string
  }
  
  /**
   * Metrics configuration
   */
  metrics?: {
    /** Enable/disable metrics collection (default: true) */
    enabled?: boolean
    /** Maximum number of endpoint-specific stats to track (default: 100) */
    maxEndpoints?: number
  }
  
  /**
   * Debug configuration
   */
  debug?: {
    /** Enable verbose logging (default: false) */
    verbose?: boolean
    /** Log failed requests (default: true in development) */
    logErrors?: boolean
    /** Log successful requests (default: false) */
    logSuccess?: boolean
  }
}

/**
 * Default configuration - these work great for 99% of use cases
 */
export const DEFAULT_CONFIG: Required<TypedFetchConfig> = {
  cache: {
    maxSize: 500,
    ttl: 300000, // 5 minutes
    enabled: true
  },
  retry: {
    maxAttempts: 3,
    delays: [100, 250, 500, 1000, 2000],
    retryableStatuses: [408, 429, 500, 502, 503, 504]
  },
  circuit: {
    threshold: 5,
    timeout: 30000, // 30 seconds
    enabled: true
  },
  request: {
    timeout: 30000, // 30 seconds
    headers: {},
    baseURL: ''
  },
  metrics: {
    enabled: true,
    maxEndpoints: 100
  },
  debug: {
    verbose: false,
    logErrors: typeof process !== 'undefined' && process.env.NODE_ENV === 'development',
    logSuccess: false
  }
}

/**
 * Deep merge configuration helper
 */
export function mergeConfig(
  base: Required<TypedFetchConfig>, 
  override: TypedFetchConfig
): Required<TypedFetchConfig> {
  const result = { ...base }
  
  for (const key in override) {
    const overrideValue = override[key as keyof TypedFetchConfig]
    if (overrideValue && typeof overrideValue === 'object' && !Array.isArray(overrideValue)) {
      result[key as keyof TypedFetchConfig] = {
        ...base[key as keyof TypedFetchConfig],
        ...overrideValue
      } as any
    } else if (overrideValue !== undefined) {
      result[key as keyof TypedFetchConfig] = overrideValue as any
    }
  }
  
  return result
}