/**
 * Research form handling with settings management
 */

document.addEventListener('DOMContentLoaded', function() {
    // Initialize the research form
    console.log('DOM loaded, initializing research form');
    initResearchForm();
});

/**
 * Initialize the research form with values from settings
 */
function initResearchForm() {
    console.log('Initializing research form...');
    // Get form elements
    const iterationsInput = document.getElementById('iterations');
    const questionsInput = document.getElementById('questions_per_iteration');

    // Fetch all settings at once (more efficient)
    fetch('/research/settings/api')
        .then(response => {
            if (!response.ok) {
                throw new Error('Failed to fetch settings');
            }
            return response.json();
        })
        .then(data => {
            console.log('Loaded settings:', data);
            if (data && data.status === 'success' && data.settings) {
                // Find our specific settings
                const settings = data.settings;

                // Look for the iterations setting
                for (const key in settings) {
                    const setting = settings[key];
                    if (key === 'search.iterations') {
                        console.log('Found iterations setting:', setting.value);
                        iterationsInput.value = setting.value;
                    }

                    if (key === 'search.questions_per_iteration') {
                        console.log('Found questions setting:', setting.value);
                        questionsInput.value = setting.value;
                    }
                }
            }
        })
        .catch(error => {
            console.warn('Error loading research settings:', error);
            // Form will use default values if settings can't be loaded
        });

    // Add our settings saving to the form submission process
    patchFormSubmitHandler();
}

/**
 * Patch the existing form submit handler to include our settings saving functionality
 */
function patchFormSubmitHandler() {
    // Get the form element
    const form = document.getElementById('research-form');
    if (!form) return;

    // Monitor for form submissions using the capture phase to run before other handlers
    form.addEventListener('submit', function(event) {
        // Save research settings first, before the main form handler processes the submission
        saveResearchSettings();

        // Let the event continue normally to the other handlers
    }, true); // true enables capture phase
}

/**
 * Save research settings to the database
 */
function saveResearchSettings() {
    const iterations = document.getElementById('iterations').value;
    const questions = document.getElementById('questions_per_iteration').value;

    console.log('Saving research settings:', { iterations, questions });

    // Get CSRF token
    const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');

    // Save settings
    fetch('/research/settings/save_all_settings', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': csrfToken
        },
        body: JSON.stringify({
            'search.iterations': parseInt(iterations),
            'search.questions_per_iteration': parseInt(questions)
        })
    })
    .then(response => response.json())
    .then(data => {
        console.log('Settings saved:', data);
    })
    .catch(error => {
        console.warn('Error saving research settings:', error);
    });
}
