/**
 * Controlador para modales de ayuda del módulo de citas
 * 
 * Este script gestiona la interacción con los modales de ayuda en las
 * diferentes secciones relacionadas con citas y citas recurrentes.
 */

// Esperar a que el DOM esté completamente cargado
document.addEventListener('DOMContentLoaded', function() {
    // Referencias a los botones de ayuda en las diferentes secciones
    const viewAppointmentsHelpBtn = document.getElementById('showViewAppointmentsHelp');
    const createAppointmentHelpBtn = document.getElementById('showCreateAppointmentHelp');
    const detailAppointmentHelpBtn = document.getElementById('showDetailAppointmentHelp');
    const recurringAppointmentsHelpBtn = document.getElementById('showRecurringAppointmentsHelp');
    
    // Modales correspondientes
    const viewAppointmentsHelpModal = document.getElementById('appointmentsViewHelpModal');
    const createAppointmentHelpModal = document.getElementById('appointmentsCreateHelpModal');
    const detailAppointmentHelpModal = document.getElementById('appointmentsDetailHelpModal');
    const recurringAppointmentsHelpModal = document.getElementById('recurringAppointmentsHelpModal');
    
    // Instancias de modales
    let viewAppointmentsModal = null;
    let createAppointmentModal = null;
    let detailAppointmentModal = null;
    let recurringAppointmentsModal = null;
    
    /**
     * Inicializar los modales de Bootstrap si existen
     */
    if (typeof bootstrap !== 'undefined') {
        if (viewAppointmentsHelpModal) {
            viewAppointmentsModal = new bootstrap.Modal(viewAppointmentsHelpModal);
        }
        if (createAppointmentHelpModal) {
            createAppointmentModal = new bootstrap.Modal(createAppointmentHelpModal);
        }
        if (detailAppointmentHelpModal) {
            detailAppointmentModal = new bootstrap.Modal(detailAppointmentHelpModal);
        }
        if (recurringAppointmentsHelpModal) {
            recurringAppointmentsModal = new bootstrap.Modal(recurringAppointmentsHelpModal);
        }
    }
    
    /**
     * Mostrar modal si es la primera visita (verificando localStorage)
     */
    function checkFirstVisit(storageKey, modalInstance, modalElement) {
        if (!modalInstance || !modalElement) return;
        
        // Verificar si ya se ha mostrado antes
        const hasSeenHelp = localStorage.getItem(storageKey);
        if (hasSeenHelp !== 'true') {
            modalInstance.show();
        }
    }
    
    /**
     * Eventos para mostrar modales al hacer clic en botones de ayuda
     */
    if (viewAppointmentsHelpBtn && viewAppointmentsModal) {
        viewAppointmentsHelpBtn.addEventListener('click', function() {
            viewAppointmentsModal.show();
        });
        
        // Verificar si es primera visita
        checkFirstVisit('hasSeenViewAppointmentsHelp', viewAppointmentsModal, viewAppointmentsHelpModal);
    }
    
    if (createAppointmentHelpBtn && createAppointmentModal) {
        createAppointmentHelpBtn.addEventListener('click', function() {
            createAppointmentModal.show();
        });
        
        // Verificar si es primera visita
        checkFirstVisit('hasSeenCreateAppointmentHelp', createAppointmentModal, createAppointmentHelpModal);
    }
    
    if (detailAppointmentHelpBtn && detailAppointmentModal) {
        detailAppointmentHelpBtn.addEventListener('click', function() {
            detailAppointmentModal.show();
        });
        
        // Verificar si es primera visita
        checkFirstVisit('hasSeenDetailAppointmentHelp', detailAppointmentModal, detailAppointmentHelpModal);
    }
    
    if (recurringAppointmentsHelpBtn && recurringAppointmentsModal) {
        recurringAppointmentsHelpBtn.addEventListener('click', function() {
            recurringAppointmentsModal.show();
        });
        
        // Verificar si es primera visita
        checkFirstVisit('hasSeenRecurringAppointmentsHelp', recurringAppointmentsModal, recurringAppointmentsHelpModal);
    }
    
    /**
     * Gestión del checkbox "No volver a mostrar"
     */
    document.querySelectorAll('.accept-help-modal').forEach(function(button) {
        button.addEventListener('click', function() {
            const storageKey = this.dataset.storageKey;
            const modalElement = this.closest('.modal');
            const checkbox = modalElement.querySelector('input[type="checkbox"]');
            
            if (checkbox && checkbox.checked) {
                localStorage.setItem(storageKey, 'true');
            }
            
            // Cerrar el modal usando jQuery si está disponible
            if (window.jQuery) {
                jQuery(modalElement).modal('hide');
            } else if (bootstrap && modalElement.id) {
                // Usar las instancias que creamos
                if (modalElement.id === 'appointmentsViewHelpModal' && viewAppointmentsModal) {
                    viewAppointmentsModal.hide();
                } else if (modalElement.id === 'appointmentsCreateHelpModal' && createAppointmentModal) {
                    createAppointmentModal.hide();
                } else if (modalElement.id === 'appointmentsDetailHelpModal' && detailAppointmentModal) {
                    detailAppointmentModal.hide();
                } else if (modalElement.id === 'recurringAppointmentsHelpModal' && recurringAppointmentsModal) {
                    recurringAppointmentsModal.hide();
                }
            }
        });
    });
});
