// Google Sign-in integration
window.GoogleSignIn = {
  initialized: false,
  callback: null,

  init(onSignIn) {
    this.callback = onSignIn;
    const clientId = window.GOOGLE_CLIENT_ID;
    if (!clientId) {
      console.warn('Google Client ID not configured');
      return;
    }
    if (window.google?.accounts?.id) {
      window.google.accounts.id.initialize({
        client_id: clientId,
        callback: (response) => this.handleCallback(response)
      });
      this.initialized = true;
    }
  },

  handleCallback(response) {
    if (response.credential && this.callback) {
      this.callback(response.credential);
    }
  },

  signIn() {
    if (window.google?.accounts?.id) {
      window.google.accounts.id.prompt((notification) => {
        if (notification.isNotDisplayed() || notification.isSkippedMoment()) {
          // Fallback: open One Tap or redirect to sign-in
          window.google.accounts.id.renderButton(
            document.getElementById('google-signin-button'),
            { theme: 'outline', size: 'large' }
          );
        }
      });
    }
  }
};
