import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../../core/api/api_providers.dart';
import '../../../core/api/guarded_api_call.dart';
import '../../auth/data/auth_providers.dart';
import '../domain/pantry_item.dart';
import '../domain/pantry_product.dart';
import 'pantry_repository.dart';

final pantryRepositoryProvider = Provider<PantryRepository>((ref) {
  return PantryRepository(ref.watch(apiClientProvider));
});

final pantryProvider = FutureProvider<List<PantryItem>>((ref) async {
  final token = await ref.watch(authStateProvider.future);
  return guardedApiCall(
    ref,
    () => ref.read(pantryRepositoryProvider).fetchPantry(token: token),
  );
});

final pantryProductsProvider = FutureProvider<List<PantryProduct>>((ref) async {
  final token = await ref.watch(authStateProvider.future);
  return guardedApiCall(
    ref,
    () => ref.read(pantryRepositoryProvider).fetchProducts(token: token),
  );
});