window.initGoogleSignIn = async function(clientId) {
    try {
        if (!clientId) {
            throw new Error('ClientId is required');
        }

        // Inicjalizacja Google Sign-In z dynamicznym ClientId
        google.accounts.id.initialize({
            client_id: clientId,
            callback: handleGoogleResponse,
            auto_select: false,
            cancel_on_tap_outside: true
        });

        // Wyświetl popup logowania
        google.accounts.id.prompt((notification) => {
            if (notification.isNotDisplayed() || notification.isSkippedMoment()) {
                console.log('Google Sign-In popup not displayed');
            }
        });

    } catch (error) {
        console.error('Google Sign-In initialization error:', error);
        DotNet.invokeMethodAsync('Bimix.UI.Shared', 'OnGoogleSignInError', error.message);
    }
};

function handleGoogleResponse(response) {
    if (response.credential) {
        // Token otrzymany - wyślij do Blazor
        DotNet.invokeMethodAsync('Bimix.UI.Shared', 'OnGoogleSignInSuccess', response.credential);
    } else {
        DotNet.invokeMethodAsync('Bimix.UI.Shared', 'OnGoogleSignInError', 'No credential received');
    }
}
