import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../../core/api/api_providers.dart';
import '../../../core/api/guarded_api_call.dart';
import '../../../features/auth/data/auth_providers.dart';
import '../domain/recipe.dart';
import '../domain/inventory_preview.dart';
import 'recipe_repository.dart';

final recipeRepositoryProvider = Provider<RecipeRepository>((ref) {
  return RecipeRepository(ref.watch(apiClientProvider));
});

final recipesProvider = FutureProvider<List<Recipe>>((ref) async {
  final token = await ref.watch(authStateProvider.future);
  return guardedApiCall(
    ref,
    () => ref.read(recipeRepositoryProvider).fetchRecipes(token: token),
  );
});

final recipeDetailProvider =
    FutureProvider.family<Recipe, int>((ref, id) async {
  final token = await ref.watch(authStateProvider.future);
  return guardedApiCall(
    ref,
    () => ref.read(recipeRepositoryProvider).fetchRecipeDetail(id, token: token),
  );
});

final inventoryPreviewProvider =
    FutureProvider.family<InventoryPreview, int>((ref, id) async {
  final token = await ref.watch(authStateProvider.future);
  return guardedApiCall(
    ref,
    () => ref
        .read(recipeRepositoryProvider)
        .fetchInventoryPreview(id, token: token),
  );
});
