import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'app_state_provider.dart';
import '../../shared/providers/network_provider.dart';
import '../../shared/providers/error_provider.dart';
import '../../features/auth/providers/auth_provider.dart' as auth;
import '../../features/vocabulary/providers/vocabulary_provider.dart' as vocab;
import '../../features/comprehensive_test/providers/test_riverpod_provider.dart' as test;

/// 全局Provider配置
class GlobalProviders {
  static final List<Override> overrides = [
    // 这里可以添加测试时的Provider覆盖
  ];
  
  static final List<ProviderObserver> observers = [
    ProviderLogger(),
  ];

  /// 获取所有核心Provider
  static List<ProviderBase> get coreProviders => [
    // 应用状态
    appStateProvider,
    networkProvider,
    errorProvider,
    
    // 认证相关
    auth.authProvider,
    
    // 词汇相关
    vocab.vocabularyProvider,
    
    // 综合测试相关
    test.testProvider,
  ];
  
  /// 预加载Provider
  static Future<void> preloadProviders(ProviderContainer container) async {
    // 预加载网络状态
    container.read(networkProvider.notifier).refreshNetworkStatus();
    
    // 认证状态会在AuthNotifier构造时自动检查
    // 这里只需要读取provider来触发初始化
    container.read(auth.authProvider);
  }
}

/// Provider状态监听器
class ProviderLogger extends ProviderObserver {
  @override
  void didUpdateProvider(
    ProviderBase provider,
    Object? previousValue,
    Object? newValue,
    ProviderContainer container,
  ) {
    print('Provider ${provider.name ?? provider.runtimeType} updated: $newValue');
  }

  @override
  void didAddProvider(
    ProviderBase provider,
    Object? value,
    ProviderContainer container,
  ) {
    print('Provider ${provider.name ?? provider.runtimeType} added: $value');
  }

  @override
  void didDisposeProvider(
    ProviderBase provider,
    ProviderContainer container,
  ) {
    print('Provider ${provider.name ?? provider.runtimeType} disposed');
  }

  @override
  void providerDidFail(
    ProviderBase provider,
    Object error,
    StackTrace stackTrace,
    ProviderContainer container,
  ) {
    print('Provider ${provider.name ?? provider.runtimeType} failed: $error');
  }
}