import 'package:autosos_flutter/api/order_api.dart';
import 'package:autosos_flutter/api/user_api.dart';
import 'package:autosos_flutter/api/upload_api.dart';
import 'package:autosos_flutter/model/user.dart';
import 'package:autosos_flutter/service/audio_service.dart';
import 'package:autosos_flutter/service/background_service.dart';
import 'package:autosos_flutter/service/heartbeat_service.dart';
import 'package:autosos_flutter/service/location_service.dart';
import 'package:autosos_flutter/service/order_service.dart';
import 'package:autosos_flutter/service/push_service.dart';
import 'package:autosos_flutter/service/update_service.dart';
import 'package:autosos_flutter/service/upload_service.dart';
import 'package:autosos_flutter/util/sp_util.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

/// 全局 Provider 入口（所有页面通过 ref.watch/read 拿到服务）

// ===== 单例服务 =====
final spUtilProvider = Provider<SPUtil>((_) => SPUtil());

final locationServiceProvider = Provider((_) => LocationService.instance);
final pushServiceProvider = Provider((_) => PushService.instance);
final heartbeatServiceProvider = Provider((_) => HeartbeatService.instance);
final audioServiceProvider = Provider((_) => AudioService.instance);
final backgroundServiceProvider = Provider((_) => BackgroundService.instance);
final orderServiceProvider = Provider((_) => OrderService.instance);
final uploadServiceProvider = Provider((_) => UploadService.instance);
final updateServiceProvider = Provider((_) => UpdateService.instance);

// ===== API =====
final orderApiProvider = Provider((_) => OrderApi.instance);
final userApiProvider = Provider((_) => UserApi.instance);
final uploadApiProvider = Provider((_) => UploadApi.instance);

// ===== 登录态 =====
final accessTokenProvider = StateProvider<String?>((ref) {
  return SPUtil().get('accessToken') as String?;
});

final isLoggedInProvider = Provider<bool>((ref) {
  final t = ref.watch(accessTokenProvider);
  return t != null && t.isNotEmpty;
});

/// 当前登录用户（启动时尝试从缓存恢复，进入首页后异步刷新）
final currentUserProvider = StateProvider<UserInfo?>((ref) => null);