import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:niogu_app/core/providers/app_provider.dart';
import 'package:niogu_app/core/enums/item_type.dart';
import 'package:niogu_app/features/stock_in/data/repositories/stock_in_repository_impl.dart';
import 'package:niogu_app/features/stock_in/domain/entities/stock_in.dart';
import 'package:niogu_app/features/stock_in/domain/repositories/i_stock_in_repository.dart';
import 'package:niogu_app/features/stock_in/presentation/controllers/stock_in_controller.dart';

final stockInRepositoryProvider = Provider<IStockInRepository>((ref) {
  final appDatabase = ref.watch(appDatabaseProvider);
  return StockInRepositoryImpl(appDatabase);
});

final displayItemStreamProvider = StreamProvider.family
    .autoDispose<List<DisplayItem>, ItemType>((ref, itemType) {
      final stockInRepository = ref.watch(stockInRepositoryProvider);

      final currentOutletId = ref.watch(currentOutletIdProvider);
      
      if(currentOutletId == null) {
        return Stream<List<DisplayItem>>.value([]);
      }
      
      return stockInRepository.watchItems(itemType, currentOutletId);
    });

final itemSearchProvider = StateProvider.autoDispose<String>((ref) => '');

final filteredItemProvider = Provider.family
    .autoDispose<AsyncValue<List<DisplayItem>>, ItemType>((ref, itemType) {
      final itemStreamAsync = ref.watch(displayItemStreamProvider(itemType));
      final itemSearchAsync = ref.watch(itemSearchProvider);
      return itemStreamAsync.when(
        data: (items) {
          if (itemSearchAsync.trim().isEmpty) {
            return AsyncValue.data(items);
          }

          final filteredItems = items.where((item) {
            return item.name.toLowerCase().contains(
              itemSearchAsync.toLowerCase(),
            );
          }).toList();

          return AsyncValue.data(filteredItems);
        },
        error: (error, stackTrace) {
          return AsyncValue.error(error, stackTrace);
        },
        loading: () => const AsyncValue.loading(),
      );
    });

final displayItemEmptyProvider = Provider.family
    .autoDispose<DisplayItemEmpty, ItemType>((ref, itemType) {
      final itemStreamAsync = ref.watch(displayItemStreamProvider(itemType));
      final filteredItemAsync = ref.watch(filteredItemProvider(itemType));
      final itemSearchAsync = ref.watch(itemSearchProvider);

      if (itemStreamAsync.isLoading) {
        return DisplayItemEmpty.loading;
      }

      final allItems = itemStreamAsync.value ?? [];

      if (allItems.isEmpty) {
        return DisplayItemEmpty.empty_database;
      }

      final filteredItems = filteredItemAsync.value ?? [];

      if (itemSearchAsync.isNotEmpty && filteredItems.isEmpty) {
        return DisplayItemEmpty.empty_search_result;
      }

      return DisplayItemEmpty.has_data;
    });

final selectedItemProvider =
    StateNotifierProvider.family.autoDispose<
      SelectedItemController,
      Map<String, SelectedItem>, SelectedItemType
    >((ref, type) {
      return SelectedItemController(type);
    });

final stockInControllerProvider = AsyncNotifierProvider.autoDispose<StockInController, void>(
  StockInController.new,
);