switchToExisting method

Future<bool> switchToExisting()

Switches to the existing Google account selected by upgradeToGoogle, and answers whether it did.

This is separate from conflict detection so the UI can explain that guest progress cannot be transferred and obtain consent before local teardown. A player who then dismisses Google's sign-in stays the guest, with everything the guest had.

Implementation

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

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