content: import 'package:firebase_auth/firebase_auth.dart';
  import 'package:flutter/services.dart';
  import 'package:flutter_native_splash/flutter_native_splash.dart';
  import 'package:get/get.dart';
  import 'package:get_storage/get_storage.dart';
  import 'package:google_sign_in/google_sign_in.dart';
  import 'package:t_store/data/repositories/user/user_repository.dart';
  import 'package:t_store/features/authentication/screens/login/login.dart';
  import 'package:t_store/features/authentication/screens/onboarding/onboarding.dart';
  import 'package:t_store/features/authentication/screens/signup/verify_email.dart';
  import 'package:t_store/navigation_menu.dart';
  import 'package:t_store/utils/exceptions/firebase_auth_exceptions.dart';
  import 'package:t_store/utils/exceptions/firebase_exceptions.dart';
  import 'package:t_store/utils/exceptions/format_exceptions.dart';
  import 'package:t_store/utils/exceptions/platform_exceptions.dart';
  
  class AuthenticationRepository extends GetxController {
    static AuthenticationRepository get instance => Get.find();
  
    ///variables
    final deviceStorage = GetStorage();
    final _auth = FirebaseAuth.instance;
  
    ///get authenticated user data
    User? get authUser => _auth.currentUser;
  
    ///called from main.dart on app launch
    @override
    void onReady() {
      FlutterNativeSplash.remove();
      screenRedirect();
    }
  
    ///function to show relevant screen
    void screenRedirect() async {
      final user = _auth.currentUser;
      if (user != null) {
        // if the user is logged in
        if (user.emailVerified) {
          // if the user's email is verified navigate to the main Navigation menu
          Get.offAll(() => const NavigationMenu());
        } else {
          // if the user's email is not verified navigate to the VerifyEmailScreen
          Get.offAll(() => VerifyEmailScreen(email: _auth.currentUser?.email));
        }
      } else {
        //local storage
        deviceStorage.writeIfNull('isFirstTime', true);
        deviceStorage.read('isFirstTime') != true
            ? Get.offAll(() => const LoginScreen())
            : Get.offAll(const OnBoardingScreen());
      }
    }
  
    ///[email authentication] login
    Future<UserCredential> loginWithEmailAndPassword(
        String email, String password) async {
      try {
        return await _auth.signInWithEmailAndPassword(
            email: email, password: password);
      } on FirebaseAuthException catch (e) {
        throw TFirebaseAuthException(e.code).message;
      } on FirebaseException catch (e) {
        throw TFirebaseException(e.code).message;
      } on FormatException catch (_) {
        throw const TFormatException();
      } on PlatformException catch (e) {
        throw TPlatformException(e.code).message;
      } catch (e) {
        throw 'Something went wrong, Please try again';
      }
    }
  
    ///[email authentication] register
    Future<UserCredential> registerWithEmailAndPassword(
        String email, String password) async {
      try {
        return await _auth.createUserWithEmailAndPassword(
            email: email, password: password);
      } on FirebaseAuthException catch (e) {
        throw TFirebaseAuthException(e.code).message;
      } on FirebaseException catch (e) {
        throw TFirebaseException(e.code).message;
      } on FormatException catch (_) {
        throw const TFormatException();
      } on PlatformException catch (e) {
        throw TPlatformException(e.code).message;
      } catch (e) {
        throw 'Something went wrong, Please try again';
      }
    }
  
    ///email verification
    Future<void> sendEmailVerification() async {
      try {
        await _auth.currentUser?.sendEmailVerification();
      } on FirebaseAuthException catch (e) {
        throw TFirebaseAuthException(e.code).message;
      } on FirebaseException catch (e) {
        throw TFirebaseException(e.code).message;
      } on FormatException catch (_) {
        throw const TFormatException();
      } on PlatformException catch (e) {
        throw TPlatformException(e.code).message;
      } catch (e) {
        throw 'Something went wrong, Please try again';
      }
    }
  
    ///[email authentication] - FORGOT PASSWORD
    Future<void> sendPasswordResetEmail(String email) async {
      try {
        await _auth.sendPasswordResetEmail(email: email);
      } on FirebaseAuthException catch (e) {
        throw TFirebaseAuthException(e.code).message;
      } on FirebaseException catch (e) {
        throw TFirebaseException(e.code).message;
      } on FormatException catch (_) {
        throw const TFormatException();
      } on PlatformException catch (e) {
        throw TPlatformException(e.code).message;
      } catch (e) {
        throw 'Something went wrong, Please try again';
      }
    }
  
    ///[re-authenticate] - RE AUTHENTICATE USER
    Future<void> reAuthenticateWithEmailAndPassword(String email, String password) async {
      try {
       // create a credential
        AuthCredential credential = EmailAuthProvider.credential(email: email, password: password);
  
        //RE AUTHENTICATE
        await _auth.currentUser!.reauthenticateWithCredential(credential);
      } on FirebaseAuthException catch (e) {
        throw TFirebaseAuthException(e.code).message;
      } on FirebaseException catch (e) {
        throw TFirebaseException(e.code).message;
      } on FormatException catch (_) {
        throw const TFormatException();
      } on PlatformException catch (e) {
        throw TPlatformException(e.code).message;
      } catch (e) {
        throw 'Something went wrong, Please try again';
      }
    }
  
    ///[GoogleAuthentication] - Google
    Future<UserCredential?> signInWithGoogle() async {
      try {
        //trigger the authentication flow
        final GoogleSignInAccount? userAccount = await GoogleSignIn().signIn();
  
        //obtain the auth details from the request
        final GoogleSignInAuthentication? googleAuth =
            await userAccount?.authentication;
  
        //create a new credential
        final credentials = GoogleAuthProvider.credential(
            accessToken: googleAuth?.accessToken, idToken: googleAuth?.idToken);
  
        //once signed in, return the userCredential
        return await _auth.signInWithCredential(credentials);
      } on FirebaseAuthException catch (e) {
        throw TFirebaseAuthException(e.code).message;
      } on FirebaseException catch (e) {
        throw TFirebaseException(e.code).message;
      } on FormatException catch (_) {
        throw const TFormatException();
      } on PlatformException catch (e) {
        throw TPlatformException(e.code).message;
      } catch (e) {
        throw 'Something went wrong, Please try again';
      }
    }
  
    ///logout user
    Future<void> logout() async {
      try {
        await GoogleSignIn().signOut();
        await FirebaseAuth.instance.signOut();
        Get.offAll(() => const LoginScreen());
      } on FirebaseAuthException catch (e) {
        throw TFirebaseAuthException(e.code).message;
      } on FirebaseException catch (e) {
        throw TFirebaseException(e.code).message;
      } on FormatException catch (_) {
        throw const TFormatException();
      } on PlatformException catch (e) {
        throw TPlatformException(e.code).message;
      } catch (e) {
        throw 'Something went wrong, Please try again';
      }
    }
  
    /// delete user, remove user auth and firestore account
    Future<void> deleteAccount() async {
      try {
        await UserRepository.instance.removeUserRecord(_auth.currentUser!.uid);
        await _auth.currentUser?.delete();
      } on FirebaseAuthException catch (e) {
        throw TFirebaseAuthException(e.code).message;
      } on FirebaseException catch (e) {
        throw TFirebaseException(e.code).message;
      } on FormatException catch (_) {
        throw const TFormatException();
      } on PlatformException catch (e) {
        throw TPlatformException(e.code).message;
      } catch (e) {
        throw 'Something went wrong, Please try again';
      }
    }
  }
  