<?php

declare(strict_types=1);

namespace ROITheme\Public\YoutubeFacade\Infrastructure\Wordpress;

use ROITheme\Public\YoutubeFacade\Infrastructure\Services\YoutubeFacadeContentFilter;

/**
 * Registers WordPress hooks for YouTube Facade Pattern
 *
 * PageSpeed Optimization Phase 2.4:
 * - Reduces TBT by ~2000ms by lazy-loading YouTube iframes
 * - Shows thumbnail + play button instead of full iframe
 * - Loads real iframe only on user click
 *
 * @package ROITheme\Public\YoutubeFacade
 * @since 1.0.6
 */
final class YoutubeFacadeHooksRegistrar
{
    private YoutubeFacadeContentFilter $contentFilter;

    public function __construct(YoutubeFacadeContentFilter $contentFilter)
    {
        $this->contentFilter = $contentFilter;
    }

    /**
     * Register all hooks
     */
    public function register(): void
    {
        // Filter post content to replace YouTube iframes with facades
        // Priority 101 = after rcp_filter_restricted_content (100)
        add_filter('the_content', [$this->contentFilter, 'filter'], 101);

        // Enqueue facade assets
        add_action('wp_enqueue_scripts', [$this, 'enqueueAssets'], 15);
    }

    /**
     * Enqueue CSS and JS for YouTube facades
     */
    public function enqueueAssets(): void
    {
        // Load on all frontend pages (condition removed for debugging)
        // TODO: Re-add is_singular() check after confirming assets load
        if (is_admin()) {
            return;
        }

        // DIFERIDO: Fase 4.2 PageSpeed - below the fold
        wp_enqueue_style(
            'roi-youtube-facade',
            get_template_directory_uri() . '/Public/YoutubeFacade/Infrastructure/Ui/Assets/Css/youtube-facade.css',
            [],
            ROI_VERSION,
            'print' // Diferido para no bloquear renderizado
        );

        wp_enqueue_script(
            'roi-youtube-facade',
            get_template_directory_uri() . '/Public/YoutubeFacade/Infrastructure/Ui/Assets/Js/youtube-facade.js',
            [],
            ROI_VERSION,
            [
                'in_footer' => true,
                'strategy'  => 'defer',
            ]
        );
    }
}
