<?php

class Whizzie {

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

	public function init() { }

	public static function home_gadget_setup_widgets() {

		require_once ABSPATH . 'wp-admin/includes/image.php';
		require_once ABSPATH . 'wp-admin/includes/file.php';
		require_once ABSPATH . 'wp-admin/includes/media.php';

		//  Header theme options
		set_theme_mod( 'home_gadget_header_phone_number', '1800 1200 110' );
		set_theme_mod( 'home_gadget_header_advertisement_text', 'Mega Gadget Sale! Up to 40% OFF – Limited Time Only' );
		set_theme_mod( 'home_gadget_location_text', 'location1245new york' );
		set_theme_mod( 'home_gadget_location_link', '#' );
		set_theme_mod( 'home_gadget_header_button_text', 'Request Service' );
		set_theme_mod( 'home_gadget_header_button_url', '#' );
		set_theme_mod( 'home_gadget_phone_number_text', 'Call Us Any Time' );
		set_theme_mod( 'home_gadget_phone_number', '+123 456 7890' );


	    /* Create Menu */
            $home_gadget_menuname  = 'Main Menus';
            $home_gadget_location  = 'main-menu';

            $home_gadget_menu = wp_get_nav_menu_object( $home_gadget_menuname );

            if ( ! $home_gadget_menu ) {
            $home_gadget_menu_id = wp_create_nav_menu( $home_gadget_menuname );

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

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

			// Contact Page 
			$home_gadget_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 ( $home_gadget_about_id ) {
			wp_update_nav_menu_item( $home_gadget_menu_id, 0, array(
			'menu-item-title'     => 'contact',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $home_gadget_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

			// Electronics Experts Page 
			$home_gadget_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'  => 'Electronics Experts',
			'post_status' => 'publish',
			) );

			if ( $home_gadget_about_id ) {
			wp_update_nav_menu_item( $home_gadget_menu_id, 0, array(
			'menu-item-title'     => 'Electronics Experts',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $home_gadget_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

            // CREATE SHOP PAGE
                $home_gadget_about_title = 'Shop';
                $home_gadget_about_content = 'Lorem ipsum dolor sit amet...';

                $shop_page = get_page_by_path('shop');
                if ( !$shop_page ) {
                    $home_gadget_about = array(
                        'post_type'   => 'page',
                        'post_title'  => $home_gadget_about_title,
                        'post_content'=> $home_gadget_about_content,
                        'post_status' => 'publish',
                        'post_author' => 1,
                        'post_name'   => 'shop' 
                        );
                        $home_gadget_about_id = wp_insert_post($home_gadget_about);
                    } else {
                        $home_gadget_about_id = $shop_page->ID;
                    }

                    wp_update_nav_menu_item($home_gadget_menu_id, 0, array(
                        'menu-item-title'   => __('Shop', 'home-gadget'),
                        'menu-item-classes' => 'shop',
                        'menu-item-object-id' => $home_gadget_about_id,
                        'menu-item-object'  => 'page',
                        'menu-item-type'    => 'post_type',
                        'menu-item-status'  => 'publish'
                    ));
            
			/* Assign Menu Location */
			$home_gadget_locations = get_theme_mod( 'nav_menu_locations', array() );
			$home_gadget_locations[ $home_gadget_location ] = $home_gadget_menu_id;
			set_theme_mod( 'nav_menu_locations', $home_gadget_locations );
		}

		$home_gadget_product_categories = [
			"Smartphones" => [
				"image" => get_template_directory_uri() . '/assets/images/cat1.png',
				"products" => [
					["name" => "BlendPro X500", "price" => 5.00, "img" => get_template_directory_uri() . "/assets/images/grind1.png"],
					["name" => "Gaming PC", "price" => 5.00, "img" => get_template_directory_uri() . "/assets/images/laptop04.png"],
					["name" => "Gaming Remote", "price" => 5.00, "img" => get_template_directory_uri() . "/assets/images/playstation.png"],
					["name" => "Clock", "price" => 5.00, "img" => get_template_directory_uri() . "/assets/images/watch1.png"],
					["name" => "Steam Cooker", "price" => 5.00, "img" => get_template_directory_uri() . "/assets/images/cooker.png"],
					["name" => "Android TV", "price" => 5.00, "img" => get_template_directory_uri() . "/assets/images/laptop05.png"],
					["name" => "Headphones", "price" => 5.00, "img" => get_template_directory_uri() . "/assets/images/headphone1.png"],
					["name" => "Speaker", "price" => 5.00, "img" => get_template_directory_uri() . "/assets/images/speaker.png"]
				],
			],
			"Laptops" => [
				"image" => get_template_directory_uri() . '/assets/images/cat2.png',
			],
			"Audio Devices" => [
				"image" => get_template_directory_uri() . '/assets/images/cat3.png',
			],
			"Cameras" => [
				"image" => get_template_directory_uri() . '/assets/images/cat4.png',
			],
			"Juicer" => [
				"image" => get_template_directory_uri() . '/assets/images/cat5.png',
			],
			"Smartwatches" => [
				"image" => get_template_directory_uri() . '/assets/images/cat6.png',
			],
			"Air Conditioners" => [
				"image" => get_template_directory_uri() . '/assets/images/cat7.png',
			],
			"Refrigerators" => [
				"image" => get_template_directory_uri() . '/assets/images/cat8.png',
			],
			"Washing Machines" => [
				"image" => get_template_directory_uri() . '/assets/images/cat9.png',
			],
			"Kitchen Appliances" => [
				"image" => get_template_directory_uri() . '/assets/images/cat10.png',
			],
		];

		foreach ($home_gadget_product_categories as $cat_name => $cat_data) {

			// Create or get category
			$term = term_exists($cat_name, 'product_cat');
			if (!$term) {
				$term = wp_insert_term($cat_name, 'product_cat');
			}
			if (is_wp_error($term)) {
				error_log('Error creating category: ' . $term->get_error_message());
				continue;
			}
			$cat_id = (int) $term['term_id'];

			// Set category thumbnail
			$image_id = media_sideload_image($cat_data['image'], 0, null, 'id');
			if (!is_wp_error($image_id)) {
				update_term_meta($cat_id, 'thumbnail_id', $image_id);
			} else {
				error_log("Error loading category image for {$cat_name}: " . $image_id->get_error_message());
			}

			// Create demo products for this category
           if (!empty($cat_data['products'])) {
	          foreach ($cat_data['products'] as $p) {
		     if (get_page_by_title($p['name'], OBJECT, 'product')) continue;

		       $product_id = wp_insert_post([
			    'post_title'   => $p['name'],
			    'post_content' => 'Buy ' . $p['name'] . ' at best price.',
			    'post_status'  => 'publish',
			    'post_type'    => 'product',
		    ]);

		wp_set_object_terms($product_id, $cat_name, 'product_cat');
		update_post_meta($product_id, '_regular_price', $p['price']);
		update_post_meta($product_id, '_price', $p['price']);

		//  Add featured image directly from URL
		if (!empty($p['img'])) {
			$image_id = media_sideload_image($p['img'], $product_id, null, 'id');
			if (!is_wp_error($image_id)) {
				set_post_thumbnail($product_id, $image_id);
			} else {
				error_log("Error loading product image for {$p['name']}: " . $image_id->get_error_message());
			}
		}
	}
}

		}
	}
}
