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

    function hair_spa_salon_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
        include_once(ABSPATH . 'wp-admin/includes/plugin-install.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());
                    echo 'Error installing plugin: ' . esc_html($plugin['slug']) . ' - ' . esc_html($result->get_error_message());
                    continue;
                }
            }

            // If the plugin exists but 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());
                    echo 'Error activating plugin: ' . esc_html($plugin['slug']) . ' - ' . esc_html($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
    hair_spa_salon_import_demo_content();

     // ------- Create Nav Menu --------
    // Define menu names and locations
        $hair_spa_salon_menuname_left = 'Primary Left Menu';
        $hair_spa_salon_bpmenulocation_left = 'primary-1';
        $hair_spa_salon_menuname_right = 'Primary Right Menu';
        $hair_spa_salon_bpmenulocation_right = 'primary-2';

        // Check if the left menu exists
        $hair_spa_salon_left_menu_exists = wp_get_nav_menu_object($hair_spa_salon_menuname_left);

        if (!$hair_spa_salon_left_menu_exists) {
            $hair_spa_salon_left_menu_id = wp_create_nav_menu($hair_spa_salon_menuname_left);

            // Create Home Page
            $hair_spa_salon_home_title = 'Home';
            $hair_spa_salon_home = array(
                'post_type' => 'page',
                'post_title' => $hair_spa_salon_home_title,
                'post_content' => '',
                'post_status' => 'publish',
                'post_author' => 1,
                'post_slug' => 'home'
            );
            $hair_spa_salon_home_id = wp_insert_post($hair_spa_salon_home);
            
            // Assign Home Page Template
            add_post_meta($hair_spa_salon_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', $hair_spa_salon_home_id);
            update_option('show_on_front', 'page');

            // Add Home Page to Left Menu
            wp_update_nav_menu_item($hair_spa_salon_left_menu_id, 0, array(
                'menu-item-title' => __('Home', 'hair-spa-salon'),
                'menu-item-classes' => 'home',
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $hair_spa_salon_home_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Add 'Pages' to Left Menu
            $hair_spa_salon_pages_title = 'Pages';
            $hair_spa_salon_pages_content = '...'; // Your dummy content
            $hair_spa_salon_pages = array(
                'post_type' => 'page',
                'post_title' => $hair_spa_salon_pages_title,
                'post_content' => $hair_spa_salon_pages_content,
                'post_status' => 'publish',
                'post_author' => 1,
                'post_slug' => 'pages'
            );
            $hair_spa_salon_pages_id = wp_insert_post($hair_spa_salon_pages);
            wp_update_nav_menu_item($hair_spa_salon_left_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'hair-spa-salon'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $hair_spa_salon_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Add 'About Us' to Left Menu
            $hair_spa_salon_about_title = 'About Us';
            $hair_spa_salon_about_content = '...'; // Your dummy content
            $hair_spa_salon_about = array(
                'post_type' => 'page',
                'post_title' => $hair_spa_salon_about_title,
                'post_content' => $hair_spa_salon_about_content,
                'post_status' => 'publish',
                'post_author' => 1,
                'post_slug' => 'about-us'
            );
            $hair_spa_salon_about_id = wp_insert_post($hair_spa_salon_about);
            wp_update_nav_menu_item($hair_spa_salon_left_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'hair-spa-salon'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $hair_spa_salon_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

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

        // Check if the right menu exists
        $hair_spa_salon_right_menu_exists = wp_get_nav_menu_object($hair_spa_salon_menuname_right);

        if (!$hair_spa_salon_right_menu_exists) {
            $hair_spa_salon_right_menu_id = wp_create_nav_menu($hair_spa_salon_menuname_right);

            // Add Home Page to Right Menu (can be the same page or different logic if needed)
            wp_update_nav_menu_item($hair_spa_salon_right_menu_id, 0, array(
                'menu-item-title' => __('Home', 'hair-spa-salon'),
                'menu-item-classes' => 'home',
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $hair_spa_salon_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($hair_spa_salon_right_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'hair-spa-salon'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $hair_spa_salon_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($hair_spa_salon_right_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'hair-spa-salon'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $hair_spa_salon_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($hair_spa_salon_bpmenulocation_right)) {
                $locations = get_theme_mod('nav_menu_locations');
                if (empty($locations)) {
                    $locations = array();
                }
                $locations[$hair_spa_salon_bpmenulocation_right] = $hair_spa_salon_right_menu_id;
                set_theme_mod('nav_menu_locations', $locations);
            }
        }

    //---Header--//
        set_theme_mod('hair_spa_salon_mail_text', 'EMAIL');
        set_theme_mod('hair_spa_salon_mail', 'support@example.com');
        set_theme_mod('hair_spa_salon_call_text', 'PHONE');
        set_theme_mod('hair_spa_salon_call', '+123456789');

        // Slider Section
        set_theme_mod('hair_spa_salon_slider_arrows', true);
        set_theme_mod('hair_spa_salon_slider_short_heading', 'Welcome To Hair Salon');

        for ($i = 1; $i <= 4; $i++) {
            $hair_spa_salon_title = 'We Will Make You Stylish';
            $hair_spa_salon_content = 'There are many variation of passages are Ipsum available, majority have suffered alteration in some form.';

            // Create post object
            $my_post = array(
                'post_title'    => wp_strip_all_tags($hair_spa_salon_title),
                'post_content'  => $hair_spa_salon_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('hair_spa_salon_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);
                }
            }
        }

        // services
        set_theme_mod('hair_spa_salon_services_short_heading', 'Our Services');
        set_theme_mod('hair_spa_salon_services_tittle', 'What We Are Offering');

        set_theme_mod('hair_spa_salon_service_image1', get_template_directory_uri().'/assets/images/post-img1.png' );
        set_theme_mod('hair_spa_salon_service_image2', get_template_directory_uri().'/assets/images/post-img2.png' );
        set_theme_mod('hair_spa_salon_service_image3', get_template_directory_uri().'/assets/images/post-img3.png' );
        set_theme_mod('hair_spa_salon_service_image4', get_template_directory_uri().'/assets/images/post-img4.png' );
        set_theme_mod('hair_spa_salon_service_image5', get_template_directory_uri().'/assets/images/post-img5.png' );
        set_theme_mod('hair_spa_salon_service_image6', get_template_directory_uri().'/assets/images/post-img6.png' );

        set_theme_mod('hair_spa_salon_category_box_icon1', 'fas fa-cut');
        set_theme_mod('hair_spa_salon_category_box_icon2', 'fas fa-shower');
        set_theme_mod('hair_spa_salon_category_box_icon3', 'fas fa-spa');
        set_theme_mod('hair_spa_salon_category_box_icon4', 'fas fa-hot-tub');
        set_theme_mod('hair_spa_salon_category_box_icon5', 'fas fa-hand-sparkles');
        set_theme_mod('hair_spa_salon_category_box_icon6', 'far fa-heart');

        set_theme_mod('hair_spa_salon_category_box_title1', 'Trend Haircut');
        set_theme_mod('hair_spa_salon_category_box_title2', 'Hair Wash');
        set_theme_mod('hair_spa_salon_category_box_title3', 'Hair Color');
        set_theme_mod('hair_spa_salon_category_box_title4', 'Head Massage');
        set_theme_mod('hair_spa_salon_category_box_title5', 'Facials');
        set_theme_mod('hair_spa_salon_category_box_title6', 'Manicure');

        set_theme_mod('hair_spa_salon_category_box_title_url1', '#');
        set_theme_mod('hair_spa_salon_category_box_title_url2', '#');
        set_theme_mod('hair_spa_salon_category_box_title_url3', '#');
        set_theme_mod('hair_spa_salon_category_box_title_url4', '#');
        set_theme_mod('hair_spa_salon_category_box_title_url5', '#');
        set_theme_mod('hair_spa_salon_category_box_title_url6', '#');
    }
?>