import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'auth_provider.dart';
import '../services/health_service.dart';
import '../services/admin_service.dart';
import '../services/in_app_notification_service.dart';

final exerciseServiceProvider = Provider<ExerciseService>((ref) {
  return ExerciseService(ref.watch(apiClientProvider));
});

/// 健康数据服务
final healthServiceProvider = Provider<HealthService>((ref) {
  return HealthService(ref.watch(apiClientProvider));
});

final userServiceProvider = Provider<UserService>((ref) {
  return UserService(ref.watch(apiClientProvider));
});

final medicationServiceProvider = Provider<MedicationService>((ref) {
  return MedicationService(ref.watch(apiClientProvider));
});

final dietServiceProvider = Provider<DietService>((ref) {
  return DietService(ref.watch(apiClientProvider));
});

final followUpServiceProvider = Provider<FollowUpService>((ref) {
  return FollowUpService(ref.watch(apiClientProvider));
});

final consultationServiceProvider = Provider<ConsultationService>((ref) {
  return ConsultationService(ref.watch(apiClientProvider));
});

final adminServiceProvider = Provider<AdminService>((ref) {
  return AdminService(ref.watch(apiClientProvider));
});

final inAppNotificationServiceProvider = Provider<InAppNotificationService>((
  ref,
) {
  return InAppNotificationService(ref.watch(apiClientProvider));
});

final notificationUnreadCountProvider = FutureProvider.autoDispose<int>((
  ref,
) async {
  final history = await ref
      .watch(inAppNotificationServiceProvider)
      .getHistory();
  return history.unreadCount;
});

/// 最新健康数据 Provider
final latestHealthProvider = FutureProvider.autoDispose<Map<String, dynamic>>((
  ref,
) async {
  final service = ref.watch(healthServiceProvider);
  return service.getLatest();
});


/// 用药列表 Provider
final medicationListProvider =
    FutureProvider.autoDispose<List<Map<String, dynamic>>>((ref) async {
      final service = ref.watch(medicationServiceProvider);
      return service.getList();
    });

final medicationReminderProvider =
    FutureProvider.autoDispose<List<Map<String, dynamic>>>((ref) async {
      final service = ref.watch(medicationServiceProvider);
      return service.getReminders();
    });

/// 医生列表 Provider
final doctorListProvider = FutureProvider<List<Map<String, dynamic>>>((
  ref,
) async {
  final service = ref.watch(consultationServiceProvider);
  return service.getDoctors().timeout(const Duration(seconds: 8));
});

/// 当前运动计划 Provider
final currentExercisePlanProvider =
    FutureProvider.autoDispose<Map<String, dynamic>?>((ref) async {
      final service = ref.watch(exerciseServiceProvider);
      return service.getCurrentPlan().timeout(const Duration(seconds: 8));
    });

/// 拍照/相册直接触发（无需跳转页面）
final cameraActionProvider = NotifierProvider<CameraActionNotifier, String?>(
  CameraActionNotifier.new,
);

class CameraActionNotifier extends Notifier<String?> {
  @override
  String? build() => null;
  void trigger(String action) => state = action;
  void clear() => state = null;
}

/// 拍饮食动作触发（不用跳多余页面）
final dietActionProvider = NotifierProvider<DietActionNotifier, String?>(
  DietActionNotifier.new,
);

class DietActionNotifier extends Notifier<String?> {
  @override
  String? build() => null;
  void trigger(String action) => state = action;
  void clear() => state = null;
}
