import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:http/http.dart' as http;
import '../../../constant.dart';

import 'package:shared_preferences/shared_preferences.dart';

import '../../../Const/api_config.dart';
import '../../../Repository/constant_functions.dart';
import '../../../currency.dart';

class LogOutRepo {
  Future<void> signOut({BuildContext? context}) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.remove("token");
    await prefs.remove("hasShownExpiredDialog");
    CurrencyMethods().removeCurrencyFromLocalDatabase();
    EasyLoading.showSuccess('Successfully Logged Out');
    try {
      restartApp();
    } catch (e) {
      debugPrint('Error restarting app: $e');
    }
    if (context != null && context.mounted) {
      Navigator.pushNamedAndRemoveUntil(context, '/signIn', (route) => false);
    }
  }

  Future<void> signOutApi({BuildContext? context}) async {
    final uri = Uri.parse('${APIConfig.url}/sign-out');

    try {
      await http.get(uri, headers: {
        'Accept': 'application/json',
        'Authorization': await getAuthToken(),
      });
    } catch (e) {
      debugPrint('Error during sign-out API call: $e');
    }
    await signOut(context: context);
  }
}
