import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:im_app/domain/usecases/insert_users_use_case.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:im_app/app/di/db_provider.dart';
import 'package:im_app/data/repositories/user_repository_impl.dart';
import 'package:im_app/domain/entities/user.dart';
import 'package:im_app/domain/repositories/user_repository.dart';

part 'user_provider.g.dart';

// ── Repository ────────────────────────────────────────────────────────────────

@riverpod
UserRepository userRepository(Ref ref) {
  return UserRepositoryImpl(ref.watch(storageSdkProvider));
}

// ── Use Cases ─────────────────────────────────────────────────────────────────

/// 批量插入用户用例 Provider
///
/// 封装去重 + 分批插入逻辑，ViewModel 只需传入用户列表
final insertUsersUseCaseProvider = Provider<InsertUsersUseCase>((ref) {
  return InsertUsersUseCase(userRepository: ref.read(userRepositoryProvider));
});

// ── Streams ───────────────────────────────────────────────────────────────────

@riverpod
Stream<List<User>> users(Ref ref, Set<int> uids) {
  return ref.watch(userRepositoryProvider).watchUsers(uids.toList());
}

@riverpod
Stream<List<User>> allUsers(Ref ref) {
  return ref.watch(userRepositoryProvider).watchAllUsers();
}
