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<int>((ref) async {
  final history = await ref
      .watch(inAppNotificationServiceProvider)
      .getHistory();
  return history.unreadCount;
});

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

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

final exercisePlansProvider = FutureProvider<List<Map<String, dynamic>>>((
  ref,
) async {
  return ref.watch(exerciseServiceProvider).getPlans();
});

final followUpListProvider = FutureProvider<List<Map<String, dynamic>>>((
  ref,
) async {
  return ref.watch(followUpServiceProvider).getList();
});

final dietRecordsProvider = FutureProvider<List<Map<String, dynamic>>>((
  ref,
) async {
  return ref.watch(dietServiceProvider).getRecords();
});

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

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

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

typedef TodayHealthSnapshot = ({
  Map<String, dynamic> health,
  List<Map<String, dynamic>> reminders,
  Map<String, dynamic>? exercisePlan,
});

/// 首页今日健康一次性快照。依赖项即使先后返回，也只在全部完成后整体更新。
final todayHealthSnapshotProvider = FutureProvider<TodayHealthSnapshot>((
  ref,
) async {
  final values = await Future.wait<dynamic>([
    ref.watch(latestHealthProvider.future),
    ref.watch(medicationReminderProvider.future),
    ref.watch(currentExercisePlanProvider.future),
  ]);
  return (
    health: values[0] as Map<String, dynamic>,
    reminders: values[1] as List<Map<String, dynamic>>,
    exercisePlan: values[2] as Map<String, dynamic>?,
  );
});

/// 拍照/相册直接触发（无需跳转页面）
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;
}
