/**
 * Examples of using the Global CSRF Manager
 * Remove this file in production - it's just for demonstration
 */

// Example 1: Using the global CSRFManager directly
async function exampleDirectUsage() {
    try {
        // Simple POST request with automatic CSRF handling
        const response = await CSRFManager.handleRequest('/api/endpoint', {
            method: 'POST',
            body: JSON.stringify({ data: 'example' })
        });
        
        if (response.ok) {
            const data = await response.json();
            console.log('Success:', data);
        }
    } catch (error) {
        console.error('Request failed:', error);
    }
}

// Example 2: Manual token management
function exampleManualToken() {
    // Get current token
    const token = CSRFManager.getToken();
    console.log('Current CSRF token:', token);
    
    // Add token to existing headers
    const headers = { 'Content-Type': 'application/json' };
    CSRFManager.addTokenToHeaders(headers);
    console.log('Headers with CSRF token:', headers);
}

// Example 3: Using with jQuery (automatic setup)
function exampleJquery() {
    // jQuery AJAX is automatically configured by CSRFManager
    // No need to manually add CSRF tokens
    $.ajax({
        url: '/api/endpoint',
        method: 'POST',
        data: { example: 'data' },
        success: function(response) {
            console.log('jQuery request succeeded:', response);
        },
        error: function(xhr, status, error) {
            console.error('jQuery request failed:', error);
        }
    });
}

// Example 4: Using with Axios (automatic setup)
function exampleAxios() {
    // Axios is automatically configured by CSRFManager
    // No need to manually add CSRF tokens
    axios.post('/api/endpoint', { example: 'data' })
        .then(response => {
            console.log('Axios request succeeded:', response.data);
        })
        .catch(error => {
            console.error('Axios request failed:', error);
        });
}

// Example 5: Traditional fetch with manual CSRF handling
async function exampleTraditionalFetch() {
    const token = CSRFManager.getToken();
    
    const response = await fetch('/api/endpoint', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRF-TOKEN': token
        },
        body: JSON.stringify({ data: 'example' })
    });
    
    // Handle 419 manually if needed
    if (response.status === 419) {
        console.log('CSRF token expired, refreshing...');
        const newToken = await CSRFManager.refreshToken();
        if (newToken) {
            // Retry the request with new token
            return fetch('/api/endpoint', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRF-TOKEN': newToken
                },
                body: JSON.stringify({ data: 'example' })
            });
        }
    }
    
    return response;
}

// Export examples for use in console
window.CSRFExamples = {
    directUsage: exampleDirectUsage,
    manualToken: exampleManualToken,
    jquery: exampleJquery,
    axios: exampleAxios,
    traditionalFetch: exampleTraditionalFetch
};