<?php

class Whizzie {

	public function __construct() {
		$this->init();
	}

	public function init()
	{
	
	}

	public static function fitness_trainer_elementor_setup_widgets(){

		set_theme_mod( 'fitness_trainer_elementor_header_text', 'lorem ipsum dummy text lorem ipsum' );
		set_theme_mod( 'fitness_trainer_elementor_header_location_text', '1870Lorem Ipsum Dolor SitAmet' );
		set_theme_mod( 'fitness_trainer_elementor_header_email_address', 'fitness@examplecom' );
		set_theme_mod( 'fitness_trainer_elementor_header_phone_number', '123 456 789' );
		set_theme_mod('fitness_trainer_elementor_social_links_settings', array(
            array(
                "link_text" => "fab fa-instagram",
                "link_url" => "www.instagram.com"
            ),
            array(
                "link_text" => "fab fa-twitter",
                "link_url" => "www.twitter.com"
            ),
            array(
                "link_text" => "fab fa-youtube",
                "link_url" => "www.youtube.com"
            ),
            array(
                "link_text" => "fab fa-linkedin-in",
                "link_url" => "www.linkedin.com"
            )
        ));

		$fitness_trainer_elementor_category_names =  array('servicecategory1');
		$fitness_trainer_elementor_title_array = array(
			array("Cycling", "Meditation", "Body Balance", "Yoga")
		);
		foreach ($fitness_trainer_elementor_category_names as $fitness_trainer_elementor_index => $fitness_trainer_elementor_category_name) {
			// Create or retrieve the post category term ID
			$fitness_trainer_elementor_term = term_exists($fitness_trainer_elementor_category_name, 'category');
			if ($fitness_trainer_elementor_term === 0 || $fitness_trainer_elementor_term === null) {
				// If the term does not exist, create it
				$fitness_trainer_elementor_term = wp_insert_term($fitness_trainer_elementor_category_name, 'category');
			}
			if (is_wp_error($fitness_trainer_elementor_term)) {
				error_log('Error creating category: ' . $fitness_trainer_elementor_term->get_error_message());
				continue; // Skip to the next iteration if category creation fails
			}
			for ($fitness_trainer_elementor_i = 1; $fitness_trainer_elementor_i < 4; $fitness_trainer_elementor_i++) {
				// Create post content
				$fitness_trainer_elementor_title = $fitness_trainer_elementor_title_array[$fitness_trainer_elementor_index][$fitness_trainer_elementor_i];
				$fitness_trainer_elementor_content = 'Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s.';
				// Create post post object
				$fitness_trainer_elementor_my_post = array(
					'post_title'    => wp_strip_all_tags($fitness_trainer_elementor_title),
					'post_content'  => $fitness_trainer_elementor_content,
					'post_status'   => 'publish',
					'post_type'     => 'post', // Post type set to 'post'
				);
				// Insert the post into the database
				$fitness_trainer_elementor_post_id = wp_insert_post($fitness_trainer_elementor_my_post);
				if (is_wp_error($fitness_trainer_elementor_post_id)) {
					error_log('Error creating post: ' . $fitness_trainer_elementor_post_id->get_error_message());
					continue; // Skip to the next post if creation fails
				}
				// Assign the category to the post
				wp_set_post_categories($fitness_trainer_elementor_post_id, array((int)$fitness_trainer_elementor_term['term_id']));
				// Handle the featured image using media_sideload_image
				$fitness_trainer_elementor_image_url = get_template_directory_uri() . '/assets/images/blog/blog' . ($fitness_trainer_elementor_i + 1) . '.png';
				$fitness_trainer_elementor_image_id = media_sideload_image($fitness_trainer_elementor_image_url, $fitness_trainer_elementor_post_id, null, 'id');
				if (is_wp_error($fitness_trainer_elementor_image_id)) {
					error_log('Error downloading image: ' . $fitness_trainer_elementor_image_id->get_error_message());
					continue; // Skip to the next post if image download fails
				}
				// Assign featured image to post
				set_post_thumbnail($fitness_trainer_elementor_post_id, $fitness_trainer_elementor_image_id);
			}
		}

			/* Create Menu */
            $fitness_trainer_elementor_menuname  = 'Main Menus';
            $fitness_trainer_elementor_location  = 'main-menu';

            $fitness_trainer_elementor_menu = wp_get_nav_menu_object( $fitness_trainer_elementor_menuname );

            if ( ! $fitness_trainer_elementor_menu ) {
            $fitness_trainer_elementor_menu_id = wp_create_nav_menu( $fitness_trainer_elementor_menuname );

           // Home Page 
			wp_update_nav_menu_item( $fitness_trainer_elementor_menu_id, 0, array(
				'menu-item-title'     => __( 'Home', 'fitness-trainer-elementor' ),
				'menu-item-url'       => home_url( '/' ),
				'menu-item-type'      => 'custom',
				'menu-item-status'    => 'publish',
			) );

			// About Us Page 
			$fitness_trainer_elementor_about_id = wp_insert_post( array(
			'post_type'   => 'page',
			'post_content' => 'We are committed to providing reliable, professional, and result-oriented solutions tailored to meet individual needs. Our goal is to empower people with the right guidance, knowledge, and support to help them make informed decisions for a better future. <br><br> Our mission is to deliver high-quality services with honesty, transparency, and dedication. We focus on understanding client requirements and offering practical solutions that create long-term value. <br><br> With a client-centric approach, experienced professionals, and a commitment to excellence, we ensure every individual receives the attention and guidance they deserve. We believe in building trust through quality service and consistent results.',
			'post_title'  => 'About Us',
			'post_status' => 'publish',
			) );

			if ( $fitness_trainer_elementor_about_id ) {
			wp_update_nav_menu_item( $fitness_trainer_elementor_menu_id, 0, array(
			'menu-item-title'     => 'About Us',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $fitness_trainer_elementor_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

			// Contact Us Page 
			$fitness_trainer_elementor_about_id = wp_insert_post( array(
			'post_type'   => 'page',
			'post_content' => 'We are committed to providing reliable, professional, and result-oriented solutions tailored to meet individual needs. Our goal is to empower people with the right guidance, knowledge, and support to help them make informed decisions for a better future. <br><br> Our mission is to deliver high-quality services with honesty, transparency, and dedication. We focus on understanding client requirements and offering practical solutions that create long-term value. <br><br> With a client-centric approach, experienced professionals, and a commitment to excellence, we ensure every individual receives the attention and guidance they deserve. We believe in building trust through quality service and consistent results.',
			'post_title'  => 'Contact',
			'post_status' => 'publish',
			) );

			if ( $fitness_trainer_elementor_about_id ) {
			wp_update_nav_menu_item( $fitness_trainer_elementor_menu_id, 0, array(
			'menu-item-title'     => 'contact',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $fitness_trainer_elementor_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

			// Services Page 
			$fitness_trainer_elementor_about_id = wp_insert_post( array(
			'post_type'   => 'page',
			'post_content' => 'We are committed to providing reliable, professional, and result-oriented solutions tailored to meet individual needs. Our goal is to empower people with the right guidance, knowledge, and support to help them make informed decisions for a better future. <br><br> Our mission is to deliver high-quality services with honesty, transparency, and dedication. We focus on understanding client requirements and offering practical solutions that create long-term value. <br><br> With a client-centric approach, experienced professionals, and a commitment to excellence, we ensure every individual receives the attention and guidance they deserve. We believe in building trust through quality service and consistent results.',
			'post_title'  => 'Services',
			'post_status' => 'publish',
			) );

			if ( $fitness_trainer_elementor_about_id ) {
			wp_update_nav_menu_item( $fitness_trainer_elementor_menu_id, 0, array(
			'menu-item-title'     => 'Services',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $fitness_trainer_elementor_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

         // Create Blog Page
                $fitness_trainer_elementor_blog_page_title = 'Blog';

                $fitness_trainer_elementor_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($fitness_trainer_elementor_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$fitness_trainer_elementor_blog_page_query->hae_posts()) {
                    $fitness_trainer_elementor_blog_page = array(
                        'post_type'   => 'page',
                        'post_title'  => $fitness_trainer_elementor_blog_page_title,
                        'post_status' => 'publish',
                        'post_author' => 1,
                    );
                    $fitness_trainer_elementor_blog_page_id = wp_insert_post($fitness_trainer_elementor_blog_page);
                    update_option('page_for_posts', $fitness_trainer_elementor_blog_page_id);

                    wp_update_na_menu_item($fitness_trainer_elementor_menu_id, 0, array(
                        'menu-item-title'      => __('Blog', 'fitness-trainer-elementor'),
                        'menu-item-url'        => get_permalink($fitness_trainer_elementor_blog_page_id),
                        'menu-item-status'     => 'publish',
                        'menu-item-object-id'  => $fitness_trainer_elementor_blog_page_id,
                        'menu-item-object'     => 'page',
                        'menu-item-type'       => 'post_type',
                    ));
                }
            
			/* Assign Menu Location */
			$fitness_trainer_elementor_locations = get_theme_mod( 'nav_menu_locations', array() );
			$fitness_trainer_elementor_locations[ $fitness_trainer_elementor_location ] = $fitness_trainer_elementor_menu_id;
			set_theme_mod( 'nav_menu_locations', $fitness_trainer_elementor_locations );
		}

	}
}