/**
 * @file wally-admin.js
 * @description Slim orchestrator for Wally Monitor — jQuery ready handler,
 *              event bindings, and page-specific initialization.
 *              Binds UI events to the appropriate WallyMonitor module functions.
 *              Must be loaded AFTER all other wally-*.js modules.
 * @module WallyMonitor (orchestrator)
 */
window.WallyMonitor = window.WallyMonitor || {};

(function($) {
    'use strict';

    $(document).ready(function() {

        // --- Lógica da Página de Relatório Principal ---
        if (wallyData.hook === 'toplevel_page_wally-monitor') {

            // Sincroniza segmented toggle de modo com wallyData (handled by .wally-scanner-mode-btn handler below)

            $('#wally-run-full-scan').on('click', function() {
                // Lê o modo atual do toggle ativo (fallback para wallyData)
                var $activeBtn = $('.wally-scanner-mode-btn.active');
                var selectedMode = $activeBtn.length ? $activeBtn.data('mode') : wallyData.analysisMode;
                WallyMonitor.Scanner.runFullSiteScan(selectedMode);
            });
        }

        // --- Lógica da Página de Histórico ---
        if (wallyData.hook === 'wally-monitor_page_wally-monitor-history') {
            if (typeof wallyHistoryData !== 'undefined' && wallyHistoryData.length > 0) {
                WallyMonitor.Chart.renderHistoryChart(wallyHistoryData);
            }
        }

        // --- Lógica do Editor de Post/Página ---
        if (wallyData.hook === 'post.php' || wallyData.hook === 'post-new.php') {
            // Botão para analisar manualmente
            $(document).on('click', '#wally-run-editor-scan', function() {
                WallyMonitor.Editor.runEditorScan();
            });

            // Mensagem inicial
            $('#wally-editor-results').html('<p>Clique em <strong>Analisar Acessibilidade</strong> para verificar a página completa.</p>');
        }

        // --- Lógica de UI global para o relatório ---
        $('body').on('click', '.wally-violation-header', function() {
            var $violation = $(this).closest('.wally-violation');
            $violation.toggleClass('expanded');
            $(this).next('.wally-violation-body').slideToggle(250);
        });

        // --- Scanner page segmented toggle ---
        $('body').on('click', '.wally-scanner-mode-btn', function() {
            if ($(this).hasClass('active')) return;
            $('.wally-scanner-mode-btn').removeClass('active').attr('aria-checked', 'false');
            $(this).addClass('active').attr('aria-checked', 'true');
            var mode = $(this).data('mode');
            if (typeof wallyData !== 'undefined') {
                wallyData.analysisMode = mode;
            }
        });

        // --- Expõe runEditorScan globalmente para uso no meta box SEO ---
        window.wallyRunEditorScan = WallyMonitor.Editor.runEditorScan;
    });

})(jQuery);
