/**
 * Chief Insights — device donut channel toggle + CrUX check.
 */
(function ($) {
    'use strict';

    function ajaxUrl() {
        if (typeof newsai_vars !== 'undefined' && newsai_vars.ajax_url) {
            return newsai_vars.ajax_url;
        }
        return typeof window.ajaxurl === 'string' ? window.ajaxurl : '';
    }

    function nonce() {
        return typeof newsai_vars !== 'undefined' ? newsai_vars.nonce : '';
    }

    function initDeviceDonutToggle() {
        var $seg = $('#nros-chief-device-channel');
        var $donut = $('#nros-chief-device-donut');
        if (!$seg.length || !$donut.length) {
            return;
        }
        var days = parseInt($seg.attr('data-days'), 10) || 30;
        var loading = false;

        $seg.on('click', '.nros-chief-seg__btn', function () {
            var $btn = $(this);
            var channel = String($btn.attr('data-channel') || 'web');
            if ($btn.hasClass('is-on') || loading) {
                return;
            }
            $seg.find('.nros-chief-seg__btn').removeClass('is-on');
            $btn.addClass('is-on');
            loading = true;
            $donut.addClass('is-loading');
            $.post(ajaxUrl(), {
                action: 'newsai_chief_device_donut',
                nonce: nonce(),
                days: days,
                channel: channel
            }).done(function (resp) {
                if (resp && resp.success && resp.data && resp.data.html) {
                    $donut.html(resp.data.html);
                }
            }).always(function () {
                loading = false;
                $donut.removeClass('is-loading');
            });
        });
    }

    function initCruxCheck() {
        var $btn = $('#nros-crux-check-btn');
        var $metrics = $('#nros-crux-metrics');
        if (!$btn.length || !$metrics.length) {
            return;
        }
        $btn.on('click', function () {
            if ($btn.prop('disabled')) {
                return;
            }
            var url = String($('#nros-crux-panel .nros-crux-panel__url code').text() || '').trim();
            $btn.prop('disabled', true);
            $metrics.addClass('is-loading');
            $.post(ajaxUrl(), {
                action: 'newsai_crux_check',
                nonce: nonce(),
                url: url
            }).done(function (resp) {
                if (resp && resp.success && resp.data && resp.data.html) {
                    $metrics.html(resp.data.html);
                }
            }).fail(function (xhr) {
                var msg = 'Check failed.';
                if (xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message) {
                    msg = xhr.responseJSON.data.message;
                }
                $metrics.html('<p class="nros-crux-panel__empty">' + $('<span/>').text(msg).html() + '</p>');
            }).always(function () {
                $btn.prop('disabled', false);
                $metrics.removeClass('is-loading');
            });
        });
    }

    $(function () {
        initDeviceDonutToggle();
        initCruxCheck();
    });
}(jQuery));
