import 'package:legacy_shared/legacy_shared.dart';
import 'package:account/src/features/app_users/domain/delete_app_user_use_case.dart';
import 'package:account/src/features/app_users/domain/get_app_users_use_case.dart';
import 'package:account/src/features/app_users/presentation/providers/delete_app_user_use_case_provider.dart';
import 'package:account/src/features/app_users/presentation/providers/get_app_users_use_case_provider.dart';
import 'package:account/src/features/app_users/presentation/state/app_users_view_state.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
// import 'package:sf_localizations/sf_localizations.dart';

final appUsersViewModelProvider =
NotifierProvider.autoDispose<AppUsersViewModel, AppUsersViewState>(
  AppUsersViewModel.new,
);

class AppUsersViewModel extends Notifier<AppUsersViewState> {
  late final GetAppUsersUseCase _getAppUsersUseCase;
  late final DeleteAppUserUseCase _deleteAppUserUseCase;

  @override
  AppUsersViewState build() {
    _getAppUsersUseCase = ref.read(getAppUsersUseCaseProvider);
    _deleteAppUserUseCase = ref.read(deleteAppUserUseCaseProvider);

    ref.read(loggedUserProvider.future)
        .then((user){
          setUser(user);
          return _getAppUsersUseCase.getAppUsers(userId: user.id);
        }).then(setAppUsers);

    return const AppUsersViewState();
  }

  void setUser(UserEntity user) {
    state = state.copyWith(loggedUser: user);
  }

  void setAppUsers(List<UserEntity> appUsers) {
    state = state.copyWith(
      appUsers: appUsers,
      isLoading: false,
    );
  }

  void toggleIsEditing() {
    state = state.copyWith(isEditing: !state.isEditing);
  }

  void deleteAppUser() {
    _deleteAppUserUseCase.deleteAppUser(userId: state.loggedUser!.id);
  }
}