import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../storage/secure_storage.dart';
import '../storage/local_storage.dart';
import '../network/api_client.dart';
import '../services/account_service.dart';
import '../services/multi_account_service.dart';
import '../services/referral_service.dart';
import '../services/authorization_service.dart';
import '../services/deposit_service.dart';
import '../services/wallet_service.dart';
import '../services/planting_service.dart';
import '../services/reward_service.dart';
import '../services/notification_service.dart';
import '../services/system_config_service.dart';
import '../services/leaderboard_service.dart';
import '../services/contract_signing_service.dart';
import '../services/contract_check_service.dart';
import '../services/pending_action_service.dart';
import '../services/pending_action_check_service.dart';
import '../services/pending_action_polling_service.dart';
import '../telemetry/storage/telemetry_storage.dart';
import '../../features/kyc/data/kyc_service.dart';

// Storage Providers
final secureStorageProvider = Provider<SecureStorage>((ref) {
  return SecureStorage();
});

final localStorageProvider = Provider<LocalStorage>((ref) {
  throw UnimplementedError('LocalStorage must be initialized before use');
});

// API Client Provider
final apiClientProvider = Provider<ApiClient>((ref) {
  final secureStorage = ref.watch(secureStorageProvider);
  return ApiClient(secureStorage: secureStorage);
});

// Telemetry Storage Provider
final telemetryStorageProvider = Provider<TelemetryStorage>((ref) {
  return TelemetryStorage();
});

// Multi Account Service Provider
// 注意：必须在 AccountService 之前定义，因为 AccountService 依赖它
final multiAccountServiceProvider = Provider<MultiAccountService>((ref) {
  final secureStorage = ref.watch(secureStorageProvider);
  final localStorage = ref.watch(localStorageProvider);
  final telemetryStorage = ref.watch(telemetryStorageProvider);
  return MultiAccountService(secureStorage, localStorage, telemetryStorage);
});

// Account Service Provider
final accountServiceProvider = Provider<AccountService>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  final secureStorage = ref.watch(secureStorageProvider);
  final multiAccountService = ref.watch(multiAccountServiceProvider);
  return AccountService(
    apiClient: apiClient,
    secureStorage: secureStorage,
    multiAccountService: multiAccountService,
  );
});

// Referral Service Provider
final referralServiceProvider = Provider<ReferralService>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  return ReferralService(apiClient: apiClient);
});

// Authorization Service Provider
final authorizationServiceProvider = Provider<AuthorizationService>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  return AuthorizationService(apiClient: apiClient);
});

// Deposit Service Provider
final depositServiceProvider = Provider<DepositService>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  final secureStorage = ref.watch(secureStorageProvider);
  return DepositService(apiClient: apiClient, secureStorage: secureStorage);
});

// Wallet Service Provider
final walletServiceProvider = Provider<WalletService>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  return WalletService(apiClient: apiClient);
});

// Planting Service Provider
final plantingServiceProvider = Provider<PlantingService>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  return PlantingService(apiClient: apiClient);
});

// Reward Service Provider (直接调用 reward-service)
final rewardServiceProvider = Provider<RewardService>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  return RewardService(apiClient: apiClient);
});

// Notification Service Provider (调用 admin-service)
final notificationServiceProvider = Provider<NotificationService>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  return NotificationService(apiClient: apiClient);
});

// System Config Service Provider (调用 admin-service)
final systemConfigServiceProvider = Provider<SystemConfigService>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  return SystemConfigService(apiClient: apiClient);
});

// Leaderboard Service Provider (调用 leaderboard-service)
final leaderboardServiceProvider = Provider<LeaderboardService>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  return LeaderboardService(apiClient: apiClient);
});

// Contract Signing Service Provider (调用 planting-service)
final contractSigningServiceProvider = Provider<ContractSigningService>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  return ContractSigningService(apiClient: apiClient);
});

// Contract Check Service Provider (用于启动时检查未签署合同)
final contractCheckServiceProvider = Provider<ContractCheckService>((ref) {
  final contractSigningService = ref.watch(contractSigningServiceProvider);
  return ContractCheckService(contractSigningService: contractSigningService);
});

// KYC Service Provider (调用 identity-service)
final kycServiceProvider = Provider<KycService>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  return KycService(apiClient);
});

// Pending Action Service Provider (调用 identity-service)
final pendingActionServiceProvider = Provider<PendingActionService>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  return PendingActionService(apiClient: apiClient);
});

// Pending Action Check Service Provider (用于启动时检查待办操作)
final pendingActionCheckServiceProvider = Provider<PendingActionCheckService>((ref) {
  final pendingActionService = ref.watch(pendingActionServiceProvider);
  return PendingActionCheckService(pendingActionService: pendingActionService);
});

// Pending Action Polling Service Provider (用于轮询检查待办操作)
final pendingActionPollingServiceProvider = Provider<PendingActionPollingService>((ref) {
  final checkService = ref.watch(pendingActionCheckServiceProvider);
  return PendingActionPollingService(checkService: checkService);
});

// Override provider with initialized instance
ProviderContainer createProviderContainer(LocalStorage localStorage) {
  return ProviderContainer(
    overrides: [
      localStorageProvider.overrideWithValue(localStorage),
    ],
  );
}
