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

    // Get the active theme name
    $active_theme = wp_get_theme();
    $is_author_portfolio = ($active_theme->get('Name') === 'Author Portfolio Pro');
    $page_template = $is_author_portfolio ? 'blankpage.php' : 'elementor_canvas';

    $imported_pages = [];
    foreach ($demo_pages as $page_title => $template_path) {
        // Corrected function to fetch the existing page properly
        $existing_page = rswpthemes_awt_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' => $page_template,
            ]);

            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 = rswpthemes_awt_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' => 'blankpage.php', // No Elementor template
        ]);

        if ($books_page_id) {
            $imported_pages[] = $books_page_id;
        }
    }

    $home_page = rswpthemes_awt_new_get_page_by_title('Home'); // Get the first matching page
    if ($home_page) {
        update_option('page_on_front', $home_page->ID);
        update_option('show_on_front', 'page');
    }
     // ✅ Call function to create and set menu
    rswpthemes_awt_create_demo_menu($imported_pages);
}

/**
 * Create Menu and Assign Imported Pages
 */
function rswpthemes_awt_create_demo_menu($imported_pages) {
    if (empty($imported_pages)) {
        return;
    }

    $menu_name = 'Demo Menu';
    $menu_exists = wp_get_nav_menu_object($menu_name);

    // Create the menu if it doesn't exist
    if (!$menu_exists) {
        $menu_id = wp_create_nav_menu($menu_name);
    } else {
        $menu_id = $menu_exists->term_id;
    }

    // Assign pages to the menu
    foreach ($imported_pages as $page_id) {
        wp_update_nav_menu_item($menu_id, 0, [
            'menu-item-object-id'   => $page_id,
            'menu-item-object'      => 'page',
            'menu-item-type'        => 'post_type',
            'menu-item-status'      => 'publish',
        ]);
    }

    // Assign the menu to the primary location (if the theme supports it)
    $locations = get_theme_mod('nav_menu_locations');
    if (!is_array($locations)) {
        $locations = [];
    }
    $locations['main-menu'] = $menu_id; // Assign menu to 'primary' location
    set_theme_mod('nav_menu_locations', $locations);
}

