(function() {
    'use strict';

    let isExpertMode = false;

    function initExpertMode() {
        const toggleButton = document.getElementById('expert-mode-toggle');
        if (!toggleButton) return;

        const customFormContainer = document.getElementById('custom-form-container');
        const expertFormContainer = document.getElementById('expert-form-container');

        if (!customFormContainer || !expertFormContainer) {
            console.warn('Expert mode containers not found');
            return;
        }

        toggleButton.addEventListener('click', function(e) {
            e.preventDefault();
            isExpertMode = !isExpertMode;

            const activeFormInput = document.getElementById('active-form-input');

            if (isExpertMode) {
                customFormContainer.style.display = 'none';
                expertFormContainer.style.display = 'block';
                toggleButton.textContent = 'Show Simplified Form';
                if (activeFormInput) activeFormInput.value = 'expert';
            } else {
                customFormContainer.style.display = 'block';
                expertFormContainer.style.display = 'none';
                toggleButton.textContent = 'Show Expert Mode';
                if (activeFormInput) activeFormInput.value = 'custom';
            }
        });
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initExpertMode);
    } else {
        initExpertMode();
    }

    document.addEventListener('htmx:afterSwap', function(event) {
        if (event.detail.target.id === 'service-form' || event.detail.target.classList.contains('crd-form')) {
            initExpertMode();
        }
    });
})();
