<?php
/**
 * Theme functions and definitions (site-export-6)
 */

declare( strict_types = 1 );

/**
 * 1. THEME CLEANUP & SETUP
 */
if ( ! function_exists( 'assembler_setup' ) ) :
    function assembler_setup() {
        add_editor_style( 'style.css' );
        remove_theme_support( 'core-block-patterns' );
    }
endif;
add_action( 'after_setup_theme', 'assembler_setup' );

/**
 * 2. ENQUEUE STYLES
 */
add_action( 'wp_enqueue_scripts', function() {
    wp_enqueue_style( 'theme-style', get_stylesheet_uri(), array(), wp_get_theme()->get( 'Version' ) );
});

/**
 * 3. AVA CHATBOT INTEGRATION
 */
add_action('wp_footer', function () { 
?>
<link href="https://cdn.jsdelivr.net/npm/@n8n/chat/dist/style.css" rel="stylesheet" />

<script type="module">
import { createChat } from 'https://cdn.jsdelivr.net/npm/@n8n/chat/dist/chat.bundle.es.js';

createChat({
    webhookUrl: 'https://markhickman227.app.n8n.cloud/webhook/65afb57e-1c89-44a3-b9bb-84013a4511d7/chat',
    showWelcomeScreen: true,
    title: 'Ava',
    subtitle: 'Your digital assistant',
    i18n: {
        en: {
            title: 'Ava',
            subtitle: 'How can I help today?',
            placeholder: 'Type your message...',
        }
    },
    style: {
        primaryColor: '#3b1a5a',
        backgroundColor: '#0b0712',
        buttonColor: '#3b1a5a',
    }
});

/**
 * AVA BRANDING INJECTOR
 * This pierces the Shadow DOM and applies a permanent style block.
 */
function applyAvaBranding() {
    const chatWidget = document.querySelector('n8n-chat');
    if (chatWidget && chatWidget.shadowRoot) {
        // Only inject if we haven't already
        if (!chatWidget.shadowRoot.querySelector('#ava-custom-style')) {
            const style = document.createElement('style');
            style.id = 'ava-custom-style';
            style.textContent = `
                .n8n-chat-launcher-button {
                    background-image: url('https://avaloncreativeltd.com/wp-content/uploads/2026/01/ava-chatbot.png') !important;
                    background-size: 70% !important;
                    background-repeat: no-repeat !important;
                    background-position: center !important;
                }
                .n8n-chat-launcher-button svg {
                    display: none !important;
                }
            `;
            chatWidget.shadowRoot.appendChild(style);
        }
    }
}

// Watch for the widget to load and apply branding immediately
const observer = new MutationObserver(() => {
    applyAvaBranding();
});

observer.observe(document.body, { childList: true, subtree: true });
</script>
<?php 
}, 100);
add_filter( 'wp_is_application_passwords_available', '__return_true' );