<?php
/**
 * Full-bleed renderer for SailWP Co-pilot designed pages.
 *
 * When a page has post_meta `_swcp_designed = 1`, the host theme's
 * template is swapped out for a minimal full-bleed shell so the Opus 4.7
 * design always presents at the full viewport width on any theme
 * (Cadence, Astra, GeneratePress, Divi, FSE block themes).
 *
 * Uses the canonical `template_include` filter so all WordPress hooks
 * (admin bar, rail, SEO plugins, analytics) still fire normally — only
 * the visual chrome is replaced. The earlier exit-from-template_redirect
 * approach manually fired wp_footer/wp_enqueue_scripts and could fatal
 * when a third-party plugin's hook expected a state the renderer skipped.
 */
if (!defined('ABSPATH')) exit;

class SailWP_Copilot_Fullpage_Renderer {

    /** True for the current request once we've decided to render fullpage. */
    private $is_fullpage = false;

    public function __construct() {
        // High priority so we win against page-builders / SEO plugins that
        // also try to swap the template (Elementor uses 11, Divi varies).
        add_filter('template_include', [$this, 'maybe_use_fullpage_template'], 99);
        // Dequeue host-theme stylesheets just before they print. Priority
        // 9999 so we run after every theme/plugin has finished enqueueing.
        add_action('wp_enqueue_scripts', [$this, 'strip_theme_styles'], 9999);
    }

    public function maybe_use_fullpage_template($template) {
        if (!is_singular()) return $template;
        $post = get_queried_object();
        if (!$post instanceof WP_Post) return $template;
        if (!get_post_meta($post->ID, '_swcp_designed', true)) return $template;
        $this->is_fullpage = true;
        return SAILWP_COPILOT_DIR . 'includes/templates/fullpage.php';
    }

    /**
     * Walk every enqueued style and dequeue anything that lives under the
     * active theme's stylesheet or template directory. Theme handle names
     * differ per theme (astra-theme-css, generate-style, kadence-global,
     * twenty-something-style) so URL-prefix is the only reliable signal.
     * We keep WP core / admin-bar / plugin styles (incl. the SailWP rail).
     */
    public function strip_theme_styles() {
        if (!$this->is_fullpage) return;
        global $wp_styles;
        if (!$wp_styles instanceof WP_Styles) return;

        $theme_url    = trailingslashit(get_stylesheet_directory_uri());
        $template_url = trailingslashit(get_template_directory_uri());
        $home_url     = trailingslashit(home_url());

        // Strip site-host from URLs so theme-css served from same host
        // doesn't accidentally match plugin-css under /wp-content/plugins/.
        $theme_path    = str_replace($home_url, '/', $theme_url);
        $template_path = str_replace($home_url, '/', $template_url);

        foreach ($wp_styles->registered as $handle => $reg) {
            // Keep our own rail and admin-bar so editors still see chrome.
            if ($handle === 'sailwp-copilot-rail' || $handle === 'admin-bar' || $handle === 'dashicons') continue;
            $src = isset($reg->src) ? (string) $reg->src : '';
            if (!$src) continue;
            if (
                strpos($src, $theme_url) === 0
                || strpos($src, $template_url) === 0
                || strpos($src, $theme_path) === 0
                || strpos($src, $template_path) === 0
            ) {
                wp_dequeue_style($handle);
            }
        }
    }
}
