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';
// [2026-02-17] 新增：预种计划服务（1887 USDT/份，独立于现有认种）
import '../services/pre_planting_service.dart';
// [2026-02-19] 纯新增：树转让服务（已认种树所有权转让）
import '../services/transfer_service.dart';
import '../services/reward_service.dart';
import '../services/notification_service.dart';
import '../services/system_config_service.dart';
// [2026-02-26] 新增：认种树定价配置服务（总部运营成本压力涨价，动态价格）
import '../services/tree_pricing_service.dart';
import '../services/app_asset_service.dart';
import '../services/customer_service_contact_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);
});

// [2026-02-17] Pre-Planting Service Provider (调用 planting-service / PrePlantingModule)
// 预种计划：1887 USDT/份，累计 10 份合成 1 棵树。与上方 PlantingService 完全独立。
final prePlantingServiceProvider = Provider<PrePlantingService>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  return PrePlantingService(apiClient: apiClient);
});

// [2026-02-19] Transfer Service Provider (调用 transfer-service)
// 树转让：已认种树所有权在用户间转让。与上方 PlantingService 完全独立。
final transferServiceProvider = Provider<TransferService>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  return TransferService(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);
});

// [2026-02-26] Tree Pricing Service Provider (调用 admin-service 公开接口)
// 认种树定价：动态获取当前价格（basePrice + supplement），5 分钟缓存
final treePricingServiceProvider = Provider<TreePricingService>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  return TreePricingService(apiClient: apiClient);
});

// App Asset Service Provider (调用 admin-service)
final appAssetServiceProvider = Provider<AppAssetService>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  final localStorage = ref.watch(localStorageProvider);
  return AppAssetService(apiClient: apiClient, localStorage: localStorage);
});

// Customer Service Contact Service Provider (调用 admin-service)
final customerServiceContactServiceProvider = Provider<CustomerServiceContactService>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  final localStorage = ref.watch(localStorageProvider);
  return CustomerServiceContactService(apiClient: apiClient, localStorage: localStorage);
});

// 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);
  final prePlantingService = ref.watch(prePlantingServiceProvider);
  return ContractCheckService(
    contractSigningService: contractSigningService,
    prePlantingService: prePlantingService,
  );
});

// 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),
    ],
  );
}
