import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:niogu_ecommerce_v1/core/providers/app_provider.dart';
import 'package:niogu_ecommerce_v1/features/cart/data/datasource/cart_remote_datasource_impl.dart';
import 'package:niogu_ecommerce_v1/features/cart/data/repositories/cart_repository_impl.dart';
import 'package:niogu_ecommerce_v1/features/cart/domain/entities/cart.dart';
import 'package:niogu_ecommerce_v1/features/cart/domain/repositories/i_cart_repository.dart';
import 'package:niogu_ecommerce_v1/features/cart/presentation/controllers/cart_controller.dart';
import 'package:niogu_ecommerce_v1/features/home/domain/entities/home.dart';

final cartRemoteDatasourceProvider =
    Provider.autoDispose<ICartRemoteDatasource>((ref) {
      final dio = ref.watch(dioProvider);

      return CartRemoteDatasourceImpl(dio);
    });

final cartRepositoryProvider = Provider.autoDispose<ICartRepository>((ref) {
  final cartRemoteDatasource = ref.watch(cartRemoteDatasourceProvider);

  return CartRepositoryImpl(cartRemoteDatasource);
});

final productBestSellerControllerProvider =
    AsyncNotifierProvider.autoDispose<
      ProductBestSellerController,
      List<ProductItem>
    >(ProductBestSellerController.new);

final cartItemControllerProvider =
    StateNotifierProvider<CartItemsController, Map<String, CartItem>>(
      (ref) => CartItemsController(),
    );

final cartItemProvider = Provider<List<CartItem>>((ref) {
  final currentOutletId = ref.watch(currentOutletIdProvider);

  final cartItemController = ref.watch(cartItemControllerProvider);

  return cartItemController.values
      .map((cartItem) => cartItem)
      .where((item) => item.outletId == currentOutletId)
      .toList();
});
