import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:niogu_app/core/providers/app_provider.dart';
import 'package:niogu_app/features/outlets/data/repositories/outlet_repository_impl.dart';
import 'package:niogu_app/features/outlets/domain/entities/outlet.dart';
import 'package:niogu_app/features/outlets/domain/repositories/i_outlet_repository.dart';
import 'package:niogu_app/features/outlets/presentation/controllers/outlet_controller.dart';
import 'package:niogu_app/features/stock_in/domain/entities/stock_in_transfer.dart';

final outletRepositoryProvider = Provider<IOutletRepository>((ref) {
  final appDatabase = ref.watch(appDatabaseProvider);
  return OutletRepositoryImpl(appDatabase);
});

final userDeletedByOutletStreamProvider =
    StreamProvider.autoDispose<List<UserDeletedByOutlet>>((ref) {
      final outletRepository = ref.watch(outletRepositoryProvider);

      return outletRepository.watchUserDeletedByOutlet();
    });

final limitOutletStreamProvider = StreamProvider.autoDispose<int>((ref) {
  final outletRepository = ref.watch(outletRepositoryProvider);
  return outletRepository.watchLimitOutlet();
});

final displayOutletStreamProvider =
    StreamProvider.autoDispose<List<DisplayOutlet>>((ref) {
      final outletRepository = ref.watch(outletRepositoryProvider);
      return outletRepository.watchDisplayOutlets();
    });

final displayOutletEmptyProvider = Provider.autoDispose<DisplayOutletEmpty>((
  ref,
) {
  final displayOutletStreamAsync = ref.watch(displayOutletStreamProvider);

  if (displayOutletStreamAsync.isLoading) {
    return DisplayOutletEmpty.loading;
  }

  final allOutlets = displayOutletStreamAsync.value ?? [];

  if (allOutlets.isEmpty) {
    return DisplayOutletEmpty.empty_database;
  }

  return DisplayOutletEmpty.has_data;
});

final staffAdminProvider = StreamProvider.family
    .autoDispose<List<StaffAdminDetail>, String>((ref, outletId) {
      final outletRepository = ref.watch(outletRepositoryProvider);
      return outletRepository.watchStaffAdmins(outletId);
    });

final outletStreamProvider = StreamProvider.autoDispose<List<Outlet>>((ref) {
  final isLoggedIn = ref.watch(currentStatusLoginProvider);
  
  final outletRepository = ref.watch(outletRepositoryProvider);
  
  if(!isLoggedIn) {
    return Stream<List<Outlet>>.value([]);
  }

  return outletRepository.watchOutlets();
});

final mapOutletAddressProvider = StateProvider<OutletAddressSelected?>(
  (ref) => null,
);

final sourceOutletStreamProvider = StreamProvider.family
    .autoDispose<List<SourceOutlet>, bool>((ref, isShowAll) {
      final isLoggedIn = ref.watch(currentStatusLoginProvider);

      final outletRepository = ref.watch(outletRepositoryProvider);
      
      final currentOutletId = ref.watch(currentOutletIdProvider);
      
      if(!isLoggedIn) {
        return Stream<List<SourceOutlet>>.value([]);
      }

      return outletRepository.watchSourceOutlets(currentOutletId!, isShowAll);
    });

final outletControllerProvider =
    AsyncNotifierProvider.autoDispose<OutletController, void>(
      OutletController.new,
    );
