<?php 
if (isset($_GET['import-demo']) && $_GET['import-demo'] == true) {

   // Function to install and activate plugins
    function restaurant_fast_food_import_demo_content() {

        // Display the preloader only for plugin installation
        echo '<div id="plugin-loader" style="display: flex; align-items: center; justify-content: center; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(255, 255, 255, 0.8); z-index: 9999;">
                <img src="' . esc_url(get_template_directory_uri()) . '/assets/images/loader.png" alt="Loading..." width="60" height="60" />
              </div>';

        // Define the plugins you want to install and activate
        $plugins = array(
            array(
                'slug' => 'woocommerce',
                'file' => 'woocommerce/woocommerce.php',
                'url'  => 'https://downloads.wordpress.org/plugin/woocommerce.latest-stable.zip'
            ),
            array(
                'slug' => 'advanced-appointment-booking-scheduling',
                'file' => 'advanced-appointment-booking-scheduling/advanced-appointment-booking.php',
                'url'  => 'https://downloads.wordpress.org/plugin/advanced-appointment-booking-scheduling.zip'
            ),
        );

        // Include required files for plugin installation
        if (!function_exists('plugins_api')) {
            include_once(ABSPATH . 'wp-admin/includes/plugin-install.php');
        }
        if (!function_exists('activate_plugin')) {
            include_once(ABSPATH . 'wp-admin/includes/plugin.php');
        }
        include_once(ABSPATH . 'wp-admin/includes/file.php');
        include_once(ABSPATH . 'wp-admin/includes/misc.php');
        include_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php');

        // Loop through each plugin
        foreach ($plugins as $plugin) {
            $plugin_file = WP_PLUGIN_DIR . '/' . $plugin['file'];

            // Check if the plugin is installed
            if (!file_exists($plugin_file)) {
                // If the plugin is not installed, download and install it
                $upgrader = new Plugin_Upgrader();
                $result = $upgrader->install($plugin['url']);

                // Check for installation errors
                if (is_wp_error($result)) {
                    error_log('Plugin installation failed: ' . $plugin['slug'] . ' - ' . $result->get_error_message());
                    continue;
                }
            }

            // If the plugin folder exists but the plugin is not active, activate it
            if (file_exists($plugin_file) && !is_plugin_active($plugin['file'])) {
                $result = activate_plugin($plugin['file']);

                // Check for activation errors
                if (is_wp_error($result)) {
                    error_log('Plugin activation failed: ' . $plugin['slug'] . ' - ' . $result->get_error_message());
                }
            }
        }

        // Hide the preloader after the process is complete
        echo '<script type="text/javascript">
                document.getElementById("plugin-loader").style.display = "none";
              </script>';

        // Add filter to skip WooCommerce setup wizard after activation
        add_filter('woocommerce_prevent_automatic_wizard_redirect', '__return_true');
    }

    // Call the import function
    restaurant_fast_food_import_demo_content();
    
    // ------- Create Nav Menu --------
    // Define menu names and locations
        $restaurant_fast_food_menuname_left = 'Primary Left Menu';
        $restaurant_fast_food_bpmenulocation_left = 'primary-1';
        $restaurant_fast_food_menuname_right = 'Primary Right Menu';
        $restaurant_fast_food_bpmenulocation_right = 'primary-2';

        // Check if the left menu exists
        $restaurant_fast_food_left_menu_exists = wp_get_nav_menu_object($restaurant_fast_food_menuname_left);

        if (!$restaurant_fast_food_left_menu_exists) {
            $restaurant_fast_food_left_menu_id = wp_create_nav_menu($restaurant_fast_food_menuname_left);

            // Create Home Page
            $restaurant_fast_food_home_title = 'Home';
            $restaurant_fast_food_home = array(
                'post_type' => 'page',
                'post_title' => $restaurant_fast_food_home_title,
                'post_content' => '',
                'post_status' => 'publish',
                'post_author' => 1,
                'post_slug' => 'home'
            );
            $restaurant_fast_food_home_id = wp_insert_post($restaurant_fast_food_home);
            
            // Assign Home Page Template
            add_post_meta($restaurant_fast_food_home_id, '_wp_page_template', 'page-template/front-page.php');

            // Update options to set Home Page as the front page
            update_option('page_on_front', $restaurant_fast_food_home_id);
            update_option('show_on_front', 'page');

            // Add Home Page to Left Menu
            wp_update_nav_menu_item($restaurant_fast_food_left_menu_id, 0, array(
                'menu-item-title' => __('Home', 'restaurant-fast-food'),
                'menu-item-classes' => 'home',
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $restaurant_fast_food_home_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Add 'Pages' to Left Menu
            $restaurant_fast_food_pages_title = 'Pages';
            $restaurant_fast_food_pages_content = '...'; // Your dummy content
            $restaurant_fast_food_pages = array(
                'post_type' => 'page',
                'post_title' => $restaurant_fast_food_pages_title,
                'post_content' => $restaurant_fast_food_pages_content,
                'post_status' => 'publish',
                'post_author' => 1,
                'post_slug' => 'pages'
            );
            $restaurant_fast_food_pages_id = wp_insert_post($restaurant_fast_food_pages);
            wp_update_nav_menu_item($restaurant_fast_food_left_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'restaurant-fast-food'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $restaurant_fast_food_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Add 'About Us' to Left Menu
            $restaurant_fast_food_about_title = 'About Us';
            $restaurant_fast_food_about_content = '...'; // Your dummy content
            $restaurant_fast_food_about = array(
                'post_type' => 'page',
                'post_title' => $restaurant_fast_food_about_title,
                'post_content' => $restaurant_fast_food_about_content,
                'post_status' => 'publish',
                'post_author' => 1,
                'post_slug' => 'about-us'
            );
            $restaurant_fast_food_about_id = wp_insert_post($restaurant_fast_food_about);
            wp_update_nav_menu_item($restaurant_fast_food_left_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'restaurant-fast-food'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $restaurant_fast_food_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Assign Left Menu to its location
            if (!has_nav_menu($restaurant_fast_food_bpmenulocation_left)) {
                $locations = get_theme_mod('nav_menu_locations');
                if (empty($locations)) {
                    $locations = array();
                }
                $locations[$restaurant_fast_food_bpmenulocation_left] = $restaurant_fast_food_left_menu_id;
                set_theme_mod('nav_menu_locations', $locations);
            }
        }

        // Check if the right menu exists
        $restaurant_fast_food_right_menu_exists = wp_get_nav_menu_object($restaurant_fast_food_bpmenulocation_right);

        if (!$restaurant_fast_food_right_menu_exists) {
            $restaurant_fast_food_right_menu_id = wp_create_nav_menu($restaurant_fast_food_bpmenulocation_right);

            // Add Home Page to Right Menu (can be the same page or different logic if needed)
            wp_update_nav_menu_item($restaurant_fast_food_right_menu_id, 0, array(
                'menu-item-title' => __('Home', 'restaurant-fast-food'),
                'menu-item-classes' => 'home',
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $restaurant_fast_food_home_id, // Reuse the same home ID
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Add 'Pages' to Right Menu
            wp_update_nav_menu_item($restaurant_fast_food_right_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'restaurant-fast-food'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $restaurant_fast_food_pages_id, // Reuse the same page ID
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Add 'About Us' to Right Menu
            wp_update_nav_menu_item($restaurant_fast_food_right_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'restaurant-fast-food'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $restaurant_fast_food_about_id, // Reuse the same about ID
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Assign Right Menu to its location
            if (!has_nav_menu($restaurant_fast_food_bpmenulocation_right)) {
                $locations = get_theme_mod('nav_menu_locations');
                if (empty($locations)) {
                    $locations = array();
                }
                $locations[$restaurant_fast_food_bpmenulocation_right] = $restaurant_fast_food_right_menu_id;
                set_theme_mod('nav_menu_locations', $locations);
            }
        }

        // Slider Section
        set_theme_mod('restaurant_fast_food_slider_arrows', true);

        set_theme_mod('restaurant_fast_food_slider_short_heading', 'Welcome Restaurant');

        for ($i = 1; $i <= 4; $i++) {
            $restaurant_fast_food_title = 'We Have Dialysis Food';
            $restaurant_fast_food_abt_content = 'There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration';

            // Create post object
            $my_post = array(
                'post_title'    => wp_strip_all_tags($restaurant_fast_food_title),
                'post_content'  => $restaurant_fast_food_abt_content,
                'post_status'   => 'publish',
                'post_type'     => 'page',
            );

            // Insert the post into the database
            $post_id = wp_insert_post($my_post);

            if ($post_id) {
                // Set the theme mod for the slider page
                set_theme_mod('restaurant_fast_food_slider_page' . $i, $post_id);

                $image_url = get_template_directory_uri() . '/assets/images/slider-img.png';
                $image_id = media_sideload_image($image_url, $post_id, null, 'id');

                if (!is_wp_error($image_id)) {
                    // Set the downloaded image as the post's featured image
                    set_post_thumbnail($post_id, $image_id);
                }
            }
        }

        
        // product category
        set_theme_mod( 'restaurant_fast_food_category_small_heading', 'OFFERED MENUS' );
        set_theme_mod( 'restaurant_fast_food_product_heading','Our Category' );
        set_theme_mod( 'restaurant_fast_food_prod_cat','productcategory' );

        $restaurant_fast_food_category_name = 'productcategory';
        $restaurant_fast_food_titles = array(
            'Pizza',
            'Sushi',
            'Hamburguesas',
            'Veggie',
            'Sopas',
            'Postres'
        );

        // Create or get the category
        $term = term_exists($restaurant_fast_food_category_name, 'product_cat');
        if ($term === 0 || $term === null) {
            $term = wp_insert_term($restaurant_fast_food_category_name, 'product_cat');
        }
        if (is_wp_error($term)) {
            error_log('Error creating category: ' . $term->get_error_message());
            return;
        }
        $term_id = is_array($term) ? $term['term_id'] : $term;

        // Create products
        foreach ($restaurant_fast_food_titles as $index => $title) {
            $post_id = wp_insert_post(array(
                'post_title'   => wp_strip_all_tags($title),
                'post_status'  => 'publish',
                'post_type'    => 'product',
                'post_content' => 'This is a demo product under ' . $restaurant_fast_food_category_name . '.',
            ));

            if (is_wp_error($post_id)) {
                error_log('Error creating product: ' . $post_id->get_error_message());
                continue;
            }

            wp_set_object_terms($post_id, (int)$term_id, 'product_cat');
            wp_set_object_terms($post_id, 'simple', 'product_type');

            update_post_meta($post_id, '_regular_price', '120');
            update_post_meta($post_id, '_sale_price', '80');
            update_post_meta($post_id, '_price', '80');
            update_post_meta($post_id, '_stock_status', 'instock');
            update_post_meta($post_id, '_visibility', 'visible');

            $image_url = get_template_directory_uri() . '/assets/images/product-img' . ($index + 1) . '.png';
            $image_id = media_sideload_image($image_url, $post_id, null, 'id');

            if (is_wp_error($image_id)) {
                error_log('Image error: ' . $image_id->get_error_message());
                continue;
            }

            set_post_thumbnail($post_id, $image_id);
        }


    }
?>