/**
 * EONSR AEO Agent - Frontend AI Summary Widget
 * Moves the summary widget into position and handles tab switching.
 */
document.addEventListener('DOMContentLoaded', function() {
    var widgetWrapper = document.getElementById('eonsr-aeo-ai-summary-wrapper');
    var widgetContent = widgetWrapper ? widgetWrapper.firstElementChild : null;

    var targetAbove = document.querySelector('.project-meta-row');
    var targetBelow = document.querySelector('.project-summary-button-row');

    if (widgetContent) {
        if (targetAbove) {
            targetAbove.parentNode.insertBefore(widgetContent, targetAbove.nextSibling);
            widgetWrapper.remove();
        } else if (targetBelow) {
            targetBelow.parentNode.insertBefore(widgetContent, targetBelow);
            widgetWrapper.remove();
        } else {
            widgetWrapper.style.display = 'block';
        }
    }

    var tabTriggers = document.querySelectorAll('.eonsr-aeo-tab-trigger');

    tabTriggers.forEach(function(trigger) {
        trigger.addEventListener('click', function(e) {
            e.preventDefault();
            document.querySelectorAll('.eonsr-aeo-tab-trigger').forEach(function(btn) {
                btn.classList.remove('active');
            });
            this.classList.add('active');

            var targetId = this.getAttribute('data-bs-target');
            var targetPane = document.querySelector(targetId);

            if (targetPane) {
                var parentContent = targetPane.closest('.tab-content');
                parentContent.querySelectorAll('.tab-pane').forEach(function(pane) {
                    pane.classList.remove('show', 'active');
                });
                targetPane.classList.add('show', 'active');
            }
        });
    });
});