import 'package:firebase_auth/firebase_auth.dart';
import 'package:injectable/injectable.dart';
import 'package:krow/core/application/clients/api/api_client.dart';
import 'package:krow/core/application/di/injectable.dart';
import 'package:krow/core/sevices/auth_state_service/auth_service_data_provider.dart';

enum AuthStatus {
  authenticated,
  adminValidation,
  prepareProfile,
  unauthenticated,
  error
}

@injectable
class AuthService {
  final AuthServiceDataProvider _dataProvider;

  AuthService(this._dataProvider);

  Future<AuthStatus> getAuthStatus() async {
    User? user;
    try {
      user = FirebaseAuth.instance.currentUser;
    } catch (e) {
      return AuthStatus.error;
    }

    if (user == null) {
      return AuthStatus.unauthenticated;
    } else {
      return AuthStatus.authenticated;
    }

    //todo get client? check if client is inactive - logout

    // final staffStatus = await getCachedStaffStatus();
    //
    // if (staffStatus == StaffStatus.deactivated) {
    //   return AuthStatus.unauthenticated;
    // } else if (staffStatus == StaffStatus.pending) {
    //   return AuthStatus.adminValidation;
    // } else if (staffStatus == StaffStatus.registered ||
    //     staffStatus == StaffStatus.declined) {
    //   return AuthStatus.prepareProfile;
    // } else {
    return AuthStatus.authenticated;
    // }
  }

  void logout() {
    FirebaseAuth.instance.signOut();
    getIt<ApiClient>().dropCache();
  }

  Future<void> deleteAccount() async {
    await FirebaseAuth.instance.currentUser!.delete();
    getIt<ApiClient>().dropCache();
  }
}
