<?php
function book_author_blog_setup_demo_pages($demo_pages = []) {
    if (!did_action('elementor/loaded')) {
        return;
    }

    $imported_pages = [];
    foreach ($demo_pages as $page_title => $template_path) {
        // Corrected function to fetch the existing page properly
        $existing_page = book_author_blog_new_get_page_by_title($page_title);

        if ($existing_page) {
            $existing_page_id = $existing_page->ID;
            $imported_pages[] = $existing_page->ID;
            // Check if the existing page already has the same template data
            $existing_elementor_data = get_post_meta($existing_page_id, '_elementor_data', true);

            if ($existing_elementor_data && file_exists($template_path)) {
                $json_data = file_get_contents($template_path);
                $template_data = json_decode($json_data, true);

                // Convert both to JSON for easy comparison
                $new_template_data = wp_slash(json_encode($template_data['content']));

                if ($existing_elementor_data === $new_template_data) {
                    continue; // Skip if template data is identical
                }
            }
        } else {
            // Insert new page only if it doesn't exist
            $page_id = wp_insert_post([
                'post_title'   => $page_title,
                'post_status'  => 'publish',
                'post_type'    => 'page',
                'post_content' => '',
                'page_template' => 'blankpage.php',
            ]);

            if ($page_id && file_exists($template_path)) {
                $json_data = file_get_contents($template_path);
                $template_data = json_decode($json_data, true);

                if (!empty($template_data['content'])) {
                    update_post_meta($page_id, '_elementor_data', wp_slash(json_encode($template_data['content'])));
                    update_post_meta($page_id, '_elementor_edit_mode', 'builder'); // Enable Elementor builder
                }
            }
            if ($page_id) {
                $imported_pages[] = $page_id;
            }
        }
    }

    // ✅ Insert "Books" page with Gutenberg blocks (No Elementor)
    $books_page = book_author_blog_new_get_page_by_title('Books');

    if (!$books_page) {
        $books_content = '<!-- wp:rswpbs/book-block /-->';
        $books_page_id = wp_insert_post([
            'post_title'   => 'Books',
            'post_status'  => 'publish',
            'post_type'    => 'page',
            'post_content' => $books_content, // Gutenberg content
            'page_template' => 'fullwidth.php', // No Elementor template
        ]);

        if ($books_page_id) {
            $imported_pages[] = $books_page_id;
        }
    }
    // ✅ Insert "Blog" page as a blank page and set it as the posts page
    $blog_page = book_author_blog_new_get_page_by_title('Blog');
    if (!$blog_page) {
        $blog_page_id = wp_insert_post([
            'post_title'   => 'Blog',
            'post_status'  => 'publish',
            'post_type'    => 'page',
            'post_content' => '', // Blank content
            'page_template' => 'fullwidth.php', // Use the same blank template
        ]);

        if ($blog_page_id) {
            $imported_pages[] = $blog_page_id;
            // Set this page as the posts page in WordPress
            update_option('page_for_posts', $blog_page_id);
        }
    }

    $home_page = book_author_blog_new_get_page_by_title('Home 1'); // Get the first matching page
    if ($home_page) {
        update_option('page_on_front', $home_page->ID);
        update_option('show_on_front', 'page');
    }

    book_author_blog_setup_demo_menu($imported_pages);
}