<?php

class Whizzie {

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

	public function init()
	{
	
	}

	public static function grocery_supermarket_elementor_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';

		set_theme_mod( 'grocery_supermarket_elementor_header_advertisement_text', 'Get 20% Off to your next purchase - Limites Time Only !' );

		$categories = [
			"Fresh Produce" => [
				"description" => "30% OFF",
				"image" => get_template_directory_uri() . '/assets/images/category1.png',
				"products" => [
					["title" => "Fresh Produce product", "price" => 2.99, "image" => get_template_directory_uri() . '/assets/images/product1.png']
				]
			],
			"Meat & Seafood" => [
				"description" => "20% OFF",
				"image" => get_template_directory_uri() . '/assets/images/category2.png',
				"products" => [
					["title" => "Meat & Seafood product", "price" => 5.99, "image" => get_template_directory_uri() . '/assets/images/product2.png']
				]
			],
			"Snacks & Sweets" => [
				"description" => "40% OFF",
				"image" => get_template_directory_uri() . '/assets/images/category3.png',
				"products" => [
					["title" => "Snacks & Sweets product", "price" => 2.49, "image" => get_template_directory_uri() . '/assets/images/product3.png']
				]
			],
			"Dairy Products" => [
				"description" => "30% OFF",
				"image" => get_template_directory_uri() . '/assets/images/category4.png',
				"products" => [
					["title" => "Dairy Product", "price" => 3.99, "image" => get_template_directory_uri() . '/assets/images/product4.png']
				]
			]
		];

		$default_description = "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s...";

		foreach ($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', [
					'description' => $cat_data['description']
				]);
			}
			if (is_wp_error($term)) {
				error_log('Error creating category: ' . $term->get_error_message());
				continue;
			}
			$cat_id = (int) $term['term_id'];

			// Add 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 products
			foreach ($cat_data['products'] as $prod) {
				if (!get_page_by_title($prod['title'], OBJECT, 'product')) {
					$post_id = wp_insert_post([
						'post_title' => $prod['title'],
						'post_content' => $default_description,
						'post_status' => 'publish',
						'post_type' => 'product'
					]);

					if (!is_wp_error($post_id)) {
						// Set price
						update_post_meta($post_id, '_regular_price', $prod['price']);
						update_post_meta($post_id, '_price', $prod['price']);

						// Set type
						wp_set_object_terms($post_id, 'simple', 'product_type');

						// Assign to category
						wp_set_object_terms($post_id, [$cat_name], 'product_cat');

						// Set featured image
						$product_image_id = media_sideload_image($prod['image'], $post_id, null, 'id');
						if (!is_wp_error($product_image_id)) {
							set_post_thumbnail($post_id, $product_image_id);
						} else {
							error_log("Product image error for {$prod['title']}: " . $product_image_id->get_error_message());
						}
					}
				}
			}
		}

		/* Create Menu */
            $grocery_supermarket_elementor_menuname  = 'Main Menus';
            $grocery_supermarket_elementor_location  = 'main-menu';

            $grocery_supermarket_elementor_menu = wp_get_nav_menu_object( $grocery_supermarket_elementor_menuname );

            if ( ! $grocery_supermarket_elementor_menu ) {
            $grocery_supermarket_elementor_menu_id = wp_create_nav_menu( $grocery_supermarket_elementor_menuname );

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

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

			// Offer Page 
			$grocery_supermarket_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'  => 'Offer',
			'post_status' => 'publish',
			) );

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

			// Pages Page 
			$grocery_supermarket_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'  => 'Pages',
			'post_status' => 'publish',
			) );

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

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

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

          // Create Shop Page
                $grocery_supermarket_elementor_about_title = 'Shop';
                $grocery_supermarket_elementor_about_content = 'Lorem ipsum dolor sit amet...';

                $shop_page = get_page_by_path('shop');
                if ( !$shop_page ) {
                    $grocery_supermarket_elementor_about = array(
                        'post_type'   => 'page',
                        'post_title'  => $grocery_supermarket_elementor_about_title,
                        'post_content'=> $grocery_supermarket_elementor_about_content,
                        'post_status' => 'publish',
                        'post_author' => 1,
                        'post_name'   => 'shop' 
                        );
                        $grocery_supermarket_elementor_about_id = wp_insert_post($grocery_supermarket_elementor_about);
                    } else {
                        $grocery_supermarket_elementor_about_id = $shop_page->ID;
                    }

                    wp_update_nav_menu_item($grocery_supermarket_elementor_menu_id, 0, array(
                        'menu-item-title'   => __('Shop', 'grocery-supermarket-elementor'),
                        'menu-item-classes' => 'shop',
                        'menu-item-object-id' => $grocery_supermarket_elementor_about_id,
                        'menu-item-object'  => 'page',
                        'menu-item-type'    => 'post_type',
                        'menu-item-status'  => 'publish'
                    ));
         
			/* Assign Menu Location */
			$grocery_supermarket_elementor_locations = get_theme_mod( 'nav_menu_locations', array() );
			$grocery_supermarket_elementor_locations[ $grocery_supermarket_elementor_location ] = $grocery_supermarket_elementor_menu_id;
			set_theme_mod( 'nav_menu_locations', $grocery_supermarket_elementor_locations );
		}

	}
}
 