<?php
class Whizzie {

    public function __construct() {
        add_action( 'wp_ajax_elegant_jewellery_setup_widgets', array( $this, 'elegant_jewellery_setup_widgets' ) );
    }


    public static function elegant_jewellery_setup_widgets() {

    	$shoplentor_options = array (
	        'wishlist' => 'on',
	        'compare' => 'off',
	        'ajaxsearch' => 'off',
	        'ajaxcart_singleproduct' => 'off',
	        'postduplicator' => 'off',
	        'loadproductlimit' => 20.0,
	    );
	    update_option('woolentor_others_tabs', $shoplentor_options);

        	$categories = [
			"Bracelets" => [
				"image" => get_template_directory_uri() . '/assets/images/product/bracelets.png',
				"products" => [
					["title" => "Bracelet", "price" => 2.99, "image" => get_template_directory_uri() . '/assets/images/product/product1.png']
				]
			],
			"Rings" => [
				"image" => get_template_directory_uri() . '/assets/images/product/rings.png',
				"products" => [
					["title" => "Ring", "price" => 5.99, "image" => get_template_directory_uri() . '/assets/images/product/product2.png']
				]
			],
			"Necklaces" => [
				"image" => get_template_directory_uri() . '/assets/images/product/necklaces.png',
				"products" => [
					["title" => "Necklace", "price" => 2.49, "image" => get_template_directory_uri() . '/assets/images/product/product3.png']
				]
			],
			"Earrings" => [
				"image" => get_template_directory_uri() . '/assets/images/product/earrings.png',
				"products" => [
					["title" => "Earring", "price" => 3.99, "image" => get_template_directory_uri() . '/assets/images/product/product4.png']
				]
			],
			"Engagement Rings" => [
				"image" => get_template_directory_uri() . '/assets/images/product/engagement-rings.png',
				"products" => [
					["title" => "Engagement Ring", "price" => 3.99, "image" => get_template_directory_uri() . '/assets/images/product/product5.png']
				]
			],
			"Wedding Bands" => [
				"image" => get_template_directory_uri() . '/assets/images/product/wedding-bands.png',
				"products" => [
					["title" => "Wedding Band", "price" => 3.99, "image" => get_template_directory_uri() . '/assets/images/product/product6.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 */
			$elegant_jewellerythemename = 'Elegant Jewellery';
            $elegant_jewellery_menuname  = $elegant_jewellerythemename . 'Primary Menu';
            $elegant_jewellery_location  = 'elegant-jewellery-primary-menu';

            $elegant_jewellery_menu = wp_get_nav_menu_object( $elegant_jewellery_menuname );

            if ( ! $elegant_jewellery_menu ) {
            $elegant_jewellery_menu_id = wp_create_nav_menu( $elegant_jewellery_menuname );

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

			// collections  Page 
			$elegant_jewellery_collections_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'  => 'Collections',
			'post_status' => 'publish',
			) );

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

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

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

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

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

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

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

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

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

			// Contact Us Page 
			$elegant_jewellery_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 ( $elegant_jewellery_about_id ) {
			wp_update_nav_menu_item( $elegant_jewellery_menu_id, 0, array(
			'menu-item-title'     => 'Contact Us',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $elegant_jewellery_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}
         
			/* Assign Menu Location */
			$elegant_jewellery_locations = get_theme_mod( 'nav_menu_locations', array() );
			$elegant_jewellery_locations[ $elegant_jewellery_location ] = $elegant_jewellery_menu_id;
			set_theme_mod( 'nav_menu_locations', $elegant_jewellery_locations );
		}

    }
}