import 'package:flutter/material.dart';

import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';

import 'package:pshared/provider/account.dart';
import 'package:pshared/provider/organizations.dart';
import 'package:pshared/provider/permissions.dart';

import 'package:pweb/app/router/pages.dart';
import 'package:pweb/providers/two_factor.dart';


Future<void> logoutUtil(BuildContext context) async {
  final accountProvider = context.read<AccountProvider>();
  final permissionsProvider = context.read<PermissionsProvider>();
  final organizationsProvider = context.read<OrganizationsProvider>();
  final twoFactorProvider = context.read<TwoFactorProvider>();
  await accountProvider.logout();
  permissionsProvider.reset();
  await organizationsProvider.reset();
  twoFactorProvider.reset();

  final router = GoRouter.of(context);
  final loginPath = routerPage(Pages.login);

  WidgetsBinding.instance.addPostFrameCallback((_) {
    if (!context.mounted) return;
    router.go(loginPath);
  });
}
