<?php

class Whizzie {

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

	public function init()
	{
	
	}

	public static function construction_contractor_setup_widgets(){

		set_theme_mod( 'construction_contractor_header_phone_number', '+123456789' );
		set_theme_mod( 'construction_contractor_header_email', 'construction@example.com' );
		set_theme_mod( 'construction_contractor_header_advertisement_text', 'Get Flat 15% OFF on this Elemento Website Coupon Code "ELEMENTO15"' );

		// Define category names
		$construction_contractor_category_array = array(
			"Construction Tape",
			"Construction Helmet",
			"Construction Driller",
			"Construction Screw"
		);

		// Define category images (one for each category)
		$construction_contractor_category_images = array(
			get_template_directory_uri() . '/assets/images/category1.png',
			get_template_directory_uri() . '/assets/images/category2.png',
			get_template_directory_uri() . '/assets/images/category3.png',
			get_template_directory_uri() . '/assets/images/category4.png'
		);

		// Define product content (same for all products in this example)
		$construction_contractor_product_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.';

		// Loop to create categories and one product per category
		foreach ($construction_contractor_category_array as $index => $category_name) {
			// Create or retrieve the category term
			$construction_contractor_category = term_exists($category_name, 'product_cat');
			if ($construction_contractor_category === 0 || $construction_contractor_category === null) {
				$construction_contractor_category = wp_insert_term($category_name, 'product_cat');
			}

			if (is_wp_error($construction_contractor_category)) {
				error_log('Error creating category: ' . $construction_contractor_category->get_error_message());
				continue; // Skip to the next category if creation fails
			}

			// Get the term ID of the category
			$category_id = (int) $construction_contractor_category['term_id'];

			// Add the category image (if applicable)
			if (!empty($construction_contractor_category_images[$index])) {
				$image_url = $construction_contractor_category_images[$index];
				$image_id = media_sideload_image($image_url, 0, null, 'id');

				if (!is_wp_error($image_id)) {
					// Assign the image as category thumbnail
					update_term_meta($category_id, 'thumbnail_id', $image_id);
				} else {
					error_log('Error downloading category image: ' . $image_id->get_error_message());
				}
			}

		}

		/* Create Menu */
            $construction_contractor_menuname  = 'Main Menus';
            $construction_contractor_location  = 'main-menu';

            $construction_contractor_menu = wp_get_nav_menu_object( $construction_contractor_menuname );

            if ( ! $construction_contractor_menu ) {
            $construction_contractor_menu_id = wp_create_nav_menu( $construction_contractor_menuname );

           // Home Page 
			wp_update_nav_menu_item( $construction_contractor_menu_id, 0, array(
				'menu-item-title'     => __( 'Home', 'construction-contractor' ),
				'menu-item-url'       => home_url( '/' ),
				'menu-item-type'      => 'custom',
				'menu-item-status'    => 'publish',
			) );

			// About Us Page 
			$construction_contractor_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 ( $construction_contractor_about_id ) {
			wp_update_nav_menu_item( $construction_contractor_menu_id, 0, array(
			'menu-item-title'     => 'About Us',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $construction_contractor_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

			// Services Page 
			$construction_contractor_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 ( $construction_contractor_about_id ) {
			wp_update_nav_menu_item( $construction_contractor_menu_id, 0, array(
			'menu-item-title'     => 'Services',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $construction_contractor_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

			// Contact Us Page 
			$construction_contractor_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 Us',
			'post_status' => 'publish',
			) );

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

            // Create Shop Page
                $construction_contractor_about_title = 'Shop';
                $construction_contractor_about_content = 'Lorem ipsum dolor sit amet...';

                $shop_page = get_page_by_path('shop');
                if ( !$shop_page ) {
                    $construction_contractor_about = array(
                        'post_type'   => 'page',
                        'post_title'  => $construction_contractor_about_title,
                        'post_content'=> $construction_contractor_about_content,
                        'post_status' => 'publish',
                        'post_author' => 1,
                        'post_name'   => 'shop' 
                        );
                        $construction_contractor_about_id = wp_insert_post($construction_contractor_about);
                    } else {
                        $construction_contractor_about_id = $shop_page->ID;
                    }

                    wp_update_nav_menu_item($construction_contractor_menu_id, 0, array(
                        'menu-item-title'   => __('Shop', 'construction-contractor'),
                        'menu-item-classes' => 'shop',
                        'menu-item-object-id' => $construction_contractor_about_id,
                        'menu-item-object'  => 'page',
                        'menu-item-type'    => 'post_type',
                        'menu-item-status'  => 'publish'
                    ));
            
			/* Assign Menu Location */
			$construction_contractor_locations = get_theme_mod( 'nav_menu_locations', array() );
			$construction_contractor_locations[ $construction_contractor_location ] = $construction_contractor_menu_id;
			set_theme_mod( 'nav_menu_locations', $construction_contractor_locations );
		}

	} 
}
 