<?php
/**
 * ITactics Child Theme – functions.php
 * -------------------------------------------------
 * 1. Enqueue parent + child styles
 * 2. Calendly assets (loaded on demand via shortcode)
 * 3. AOS animation speed override (fix slow page loader)
 * 4. Shortcodes: [calendly_badge], [calendly_button]
 */

/* ---------- 1. Enqueue Styles ---------- */
function itactics_child_enqueue_styles_and_assets() {
    // Enqueue Parent Theme Style
    wp_enqueue_style(
        'parent-style',
        get_template_directory_uri() . '/style.css'
    );

    // Child Theme Style — fixed version string (NOT time(), which breaks browser caching)
    wp_enqueue_style(
        'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array('parent-style'),
        '1.0.2'
    );

    // Calendly assets — only on pages that actually use a Calendly shortcode
    global $post;
    if ( is_a( $post, 'WP_Post' ) && (
        has_shortcode( $post->post_content, 'calendly_badge' ) ||
        has_shortcode( $post->post_content, 'calendly_button' )
    ) ) {
        wp_enqueue_style(
            'calendly-badge-css',
            'https://assets.calendly.com/assets/external/widget.css',
            array(),
            null
        );
        wp_enqueue_script(
            'calendly-badge-js',
            'https://assets.calendly.com/assets/external/widget.js',
            array(),
            null,
            true
        );
    }
}
add_action( 'wp_enqueue_scripts', 'itactics_child_enqueue_styles_and_assets' );


/* ---------- 2. AOS speed override ----------
 * The animate-on-scroll plugin defaults to duration:1200ms (1.2 seconds)
 * and offset:200px. This makes ALL page content invisible then slowly fade
 * in — it looks like a full page loader, especially on mobile.
 *
 * Fix: use the plugin's own 'aos_init' filter to override the params.
 * - duration: 400ms  (was 1200ms) — 3× faster animation
 * - offset:   50px   (was 200px)  — triggers much earlier on mobile screens
 * - disable:  mobile             — turns off AOS entirely on phones so
 *                                  Lighthouse mobile sees content instantly
 * ------------------------------------------*/
add_filter( 'aos_init', function( $script ) {
    // Disable AOS completely — it hides elements with opacity:0 via CSS
    // which causes NO_LCP / blank page in Lighthouse (desktop + mobile).
    // The hero/banner section is the LCP element and AOS was keeping it invisible.
    return 'var aoswp_params = {
        "offset":"50",
        "duration":"400",
        "easing":"ease",
        "delay":"0",
        "disable":true,
        "once":true};';
} );


/* ---------- 3. Calendly trigger script ---------- */
function itactics_child_calendly_generic_trigger_script() {
    global $post;
    if ( ! is_a( $post, 'WP_Post' ) || (
        ! has_shortcode( $post->post_content, 'calendly_badge' ) &&
        ! has_shortcode( $post->post_content, 'calendly_button' )
    ) ) {
        return;
    }
    $script = "
    document.addEventListener('DOMContentLoaded', function() {
        function fixPopupStructure() {
            var checkExist = setInterval(function() {
                var overlay = document.querySelector('.calendly-overlay');
                var popup = document.querySelector('.calendly-overlay .calendly-popup');
                var closeBtn = document.querySelector('.calendly-overlay .calendly-popup-close');
                if (overlay && popup && closeBtn) {
                    clearInterval(checkExist);
                    popup.appendChild(closeBtn);
                }
            }, 50);
            setTimeout(function() { clearInterval(checkExist); }, 4000);
        }
        var triggers = document.querySelectorAll('.calendly-trigger');
        triggers.forEach(function(btn) {
            btn.addEventListener('click', function(e) {
                e.preventDefault();
                var url = btn.getAttribute('data-url') || 'https://calendly.com/hi-activeroute/30min?hide_event_type_details=1&hide_gdpr_banner=1';
                Calendly.initPopupWidget({url: url});
                fixPopupStructure();
            });
        });
    });
    ";
    wp_add_inline_script('calendly-badge-js', $script);
}
add_action('wp_enqueue_scripts', 'itactics_child_calendly_generic_trigger_script');


/* ---------- 4. Shortcodes ---------- */

// [calendly_badge]
function itactics_child_calendly_badge_shortcode( $atts ) {
    $a = shortcode_atts( array(
        'url'        => 'https://calendly.com/hi-activeroute/30min?hide_event_type_details=1&hide_gdpr_banner=1',
        'text'       => 'Schedule time with me',
        'color'      => '#0069ff',
        'text_color' => '#ffffff',
        'branding'   => 'true',
    ), $atts, 'calendly_badge' );

    $script = <<<HTML
<link href="https://assets.calendly.com/assets/external/widget.css" rel="stylesheet">
<script src="https://assets.calendly.com/assets/external/widget.js" type="text/javascript" async></script>
<script type="text/javascript">
window.onload = function() {
    Calendly.initBadgeWidget({
        url: '{$a['url']}',
        text: '{$a['text']}',
        color: '{$a['color']}',
        textColor: '{$a['text_color']}',
        branding: {$a['branding']}
    });
};
</script>
HTML;
    return $script;
}
add_shortcode( 'calendly_badge', 'itactics_child_calendly_badge_shortcode' );

// [calendly_button]
function itactics_child_calendly_button_shortcode( $atts ) {
    $a = shortcode_atts( array(
        'url'        => 'https://calendly.com/hi-activeroute/30min?hide_event_type_details=1&hide_gdpr_banner=1',
        'button_text'=> 'Schedule time with me',
        'color'      => '#0069ff',
        'text_color' => '#ffffff',
        'branding'   => 'true',
    ), $atts, 'calendly_button' );

    $button = '<button class="calendly-trigger" data-url="' . esc_url($a['url']) . '" style="background-color:' . $a['color'] . ';color:' . $a['text_color'] . ';border:none;padding:8px 16px;border-radius:4px;cursor:pointer;">' . esc_html($a['button_text']) . '</button>';

    return $button;
}
add_shortcode( 'calendly_button', 'itactics_child_calendly_button_shortcode' );
