import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:niogu_app/core/providers/app_provider.dart';
import 'package:niogu_app/features/stock_in/data/repositories/stock_in_purchase_repository_impl.dart';
import 'package:niogu_app/features/stock_in/domain/entities/stock_in_purchase.dart';
import 'package:niogu_app/features/stock_in/domain/repositories/i_stock_in_purchase_repository.dart';

final proofPaymentPurchaseTempProvider = StateProvider<List<String?>>((ref) => []);

final stockInPurchaseRepositoryProvider = Provider<IStockInPurchaseRepository>((ref) {
  final appDatabase = ref.watch(appDatabaseProvider);
  return StockInPurchaseRepositoryImpl(appDatabase);
});

final alreadySupplierStreamProvider =
    StreamProvider.autoDispose<List<AlreadySupplier>>((ref) {
      final stockInPurchaseRepostiory = ref.watch(stockInPurchaseRepositoryProvider);
      return stockInPurchaseRepostiory.watchAllReadySuppliers(); 
    });

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

final filteredAlreadySupplierProvider =
    Provider.autoDispose<AsyncValue<List<AlreadySupplier>>>((ref) {
      final alreadySupplierStreamAsync = ref.watch(
        alreadySupplierStreamProvider,
      );
      final alreadySupplierSearchAsync = ref.watch(
        alreadySupplierSearchProvider,
      );

      return alreadySupplierStreamAsync.when(
        data: (suppliers) {
          if (alreadySupplierSearchAsync.trim().isEmpty) {
            return AsyncValue.data(suppliers);
          }

          final filteredSuppliers = suppliers.where((supplier) {
            return supplier.name.toLowerCase().contains(
              alreadySupplierSearchAsync.toLowerCase(),
            );
          }).toList();

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

final alreadySupplierEmptyProvider = Provider.autoDispose<AlreadySupplierEmpty>(
  (ref) {
    final alreadyCustomerStreamAsync = ref.watch(alreadySupplierStreamProvider);
    final filteredAlreadyCustomerAsync = ref.watch(
      filteredAlreadySupplierProvider,
    );
    final alreadyCustomerSearchAsync = ref.watch(alreadySupplierSearchProvider);

    if (alreadyCustomerStreamAsync.isLoading) {
      return AlreadySupplierEmpty.loading;
    }

    final allCustomers = alreadyCustomerStreamAsync.value ?? [];

    if (allCustomers.isEmpty) {
      return AlreadySupplierEmpty.empty_database;
    }

    final filteredCustomer = filteredAlreadyCustomerAsync.value ?? [];

    if (alreadyCustomerSearchAsync.isNotEmpty && filteredCustomer.isEmpty) {
      return AlreadySupplierEmpty.empty_search_result;
    }

    return AlreadySupplierEmpty.has_data;
  },
);

final selectedSupplierProvider = StateProvider.autoDispose<SelectedSupplier?>(
  (ref) => null,
);

final totalPurchaseAmountProvider = StateProvider.autoDispose<double>((ref) => 0.0);