import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../../suggestions/domain/suggestion.dart';
import '../../suggestions/infrastructure/suggestions_repository.dart';

part 'admin_suggestions_notifier.g.dart';

/// Live Firestore-backed stream of every suggestion, newest first, for the
/// admin review dashboard.
@riverpod
Stream<List<Suggestion>> adminSuggestionsStream(
  AdminSuggestionsStreamRef ref,
) {
  final repo = ref.watch(suggestionsRepositoryProvider);
  return repo.watchAllSuggestions();
}

/// Imperative wrapper around the suggestion write methods.
@riverpod
class AdminSuggestionsNotifier extends _$AdminSuggestionsNotifier {
  @override
  Future<void> build() async {}

  Future<void> updateStatus(String id, SuggestionStatus status) async {
    final repo = ref.read(suggestionsRepositoryProvider);
    state = const AsyncLoading();
    state = await AsyncValue.guard(() => repo.updateStatus(id, status));
  }

  Future<void> delete(String id) async {
    final repo = ref.read(suggestionsRepositoryProvider);
    state = const AsyncLoading();
    state = await AsyncValue.guard(() => repo.deleteSuggestion(id));
  }
}
