import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:im_app/app/di/db_provider.dart';
import 'package:im_app/data/repositories/message_repository_impl.dart';
import 'package:im_app/domain/entities/message.dart';
import 'package:im_app/domain/repositories/message_repository.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'message_provider.g.dart';

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

/// 消息仓储 Provider
final messageRepositoryProvider = Provider<MessageRepository>((ref) {
  return MessageRepositoryImpl(ref.watch(storageSdkProvider));
});

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

/// 监听指定 chatId 的消息列表
@riverpod
Stream<List<Message>> messagesByChatId(Ref ref, int chatId) {
  return ref.watch(messageRepositoryProvider).watchByChatId(chatId);
}

/// 监听指定消息
@riverpod
Stream<Message?> messageById(Ref ref, int id) {
  return ref.watch(messageRepositoryProvider).watchById(id);
}
