switchToExisting method

Future<void> switchToExisting()

Switches to the existing Google account selected by upgradeToGoogle.

This is separate from conflict detection so the UI can explain that guest progress cannot be transferred and obtain consent before local teardown.

Implementation

Future<void> switchToExisting() async {
  if (!_hasPendingExistingAccount) {
    throw StateError('No existing Google account switch is pending.');
  }

  state = const AsyncLoading();
  final guestId = ref.read(currentUserProvider)?.id;
  try {
    await ref.read(authServiceProvider).switchToExistingGoogleAccount();
    if (guestId != null) {
      try {
        await deleteUserData(ref, guestId);
      } catch (error, stackTrace) {
        developer.log(
          'Guest cache cleanup after account switch failed (ignored)',
          name: 'auth.controller',
          error: error,
          stackTrace: stackTrace,
        );
      }
    }
    state = const AsyncData(null);
  } catch (error, stackTrace) {
    state = AsyncError(error, stackTrace);
    rethrow;
  } finally {
    _hasPendingExistingAccount = false;
  }
}