<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<?php
/*
Template Name: VMO Fullwidth Template
*/

// Ultra-minimal theme wrapper that still runs wp_head/wp_footer so enqueued
// scripts/styles (Tailwind/GSAP/etc) actually load on live pages.
// Intentionally avoids get_header()/get_footer() to keep full control.

show_admin_bar(false);
add_filter('show_admin_bar', '__return_false');

if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        $post_id = get_the_ID();
        $raw_content = (string) get_post_field('post_content', $post_id);

        // Back-compat: if legacy content stored a full HTML document in post_content,
        // extract just the body for rendering. Head/footer assets are handled by
        // VMO hooks (stored meta) or echoed here as a last resort.
        $legacy_head = '';
        $legacy_footer = '';
        if (function_exists('vmo_split_html_document')) {
            $stored_head = (string) get_post_meta($post_id, 'vmo_generated_head_html', true);
            $stored_footer = (string) get_post_meta($post_id, 'vmo_generated_footer_html', true);
            $split = vmo_split_html_document($raw_content);
            if (!empty($split['is_full_document'])) {
                $raw_content = (string) ($split['body'] ?? $raw_content);
                if (empty($stored_head) && !empty($split['head'])) {
                    $legacy_head = (string) $split['head'];
                }
                if (empty($stored_footer) && !empty($split['footer'])) {
                    $legacy_footer = (string) $split['footer'];
                }
            }
        }

        ?><!doctype html>
        <html <?php language_attributes(); ?>>
        <head>
            <meta charset="<?php bloginfo('charset'); ?>" />
            <meta name="viewport" content="width=device-width, initial-scale=1" />
            <?php
            // Legacy fallback for pages published before head/footer extraction existed.
            if ($legacy_head) {
                echo $legacy_head; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
            }
            wp_head();
            ?>
        </head>
        <body <?php body_class('vmo-fullwidth'); ?>>
            <?php
            // Render raw content (no the_content filters) to preserve AI-generated markup.
            echo $raw_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

            // Legacy footer fallback.
            if ($legacy_footer) {
                echo $legacy_footer; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
            }
            wp_footer();
            ?>
        </body>
        </html>
        <?php
        // Do not include the active theme template.
        exit;
    endwhile;
endif;

exit;