<?php

class Whizzie {

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

	public function init()
	{
	
	}

	public static function designer_boutique_shop_setup_widgets(){

	set_theme_mod( 'designer_boutique_shop_header_advertisement_text', 'lorem ipsum dummy text lorem ipsum' );


		$designer_boutique_shop_product_image_gallery = array();
		$designer_boutique_shop_product_ids = array();

		$designer_boutique_shop_product_category= array(
			'Tshirts Variation'       => array(
				'Shop For Great Selection Of Tshirts',
				'Shop For Great Selection Of Shirts',
				'Shop For Great Selection Of Jeans',
			),
			'Jeans Variation'       => array(
				'Blue Half Jeans',
			),
		);
		$designer_boutique_shop_k = 1;
		foreach ( $designer_boutique_shop_product_category as $designer_boutique_shop_product_cats => $designer_boutique_shop_products_name ) { 
		// Insert porduct cats Start
		$content = 'This is sample product category';
		$designer_boutique_shop_parent_category	=	wp_insert_term(
		$designer_boutique_shop_product_cats, // the term
		'product_cat', // the taxonomy
			array(
			'description'=> $content,
			'slug' => str_replace( ' ', '-', $designer_boutique_shop_product_cats)
			)
		);

	// -------------- create subcategory END -----------------

		$designer_boutique_shop_n=1;
		// create Product START
		foreach ( $designer_boutique_shop_products_name as $key => $designer_boutique_shop_product_title ) {
		$content = '
			<div class="main_content">
			<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. </p>
			</div>';

		// Create post object
		$designer_boutique_shop_my_post = array(
			'post_title'    => wp_strip_all_tags( $designer_boutique_shop_product_title ),
			'post_content'  => $content,
			'post_status'   => 'publish',
			'post_type'     => 'product',
			'post_category' => [$designer_boutique_shop_parent_category['term_id']]
		);

		// Insert the post into the database

		$designer_boutique_shop_uqpost_id = wp_insert_post($designer_boutique_shop_my_post);
		wp_set_object_terms( $designer_boutique_shop_uqpost_id, str_replace( ' ', '-', $designer_boutique_shop_product_cats), 'product_cat', true );

		$designer_boutique_shop_product_price = array('40','30','30');
		$designer_boutique_shop_product_regular_price = array('99','98','97');
		$designer_boutique_shop_product_sale_price = array('40','30','30');
		
		update_post_meta( $designer_boutique_shop_uqpost_id, '_regular_price', $designer_boutique_shop_product_regular_price[$designer_boutique_shop_n-1] );
		update_post_meta( $designer_boutique_shop_uqpost_id, '_price', $designer_boutique_shop_product_price[$designer_boutique_shop_n-1] );
		update_post_meta( $designer_boutique_shop_uqpost_id, '_sale_price', $designer_boutique_shop_product_sale_price[$designer_boutique_shop_n-1] );
		array_push( $designer_boutique_shop_product_ids,  $designer_boutique_shop_uqpost_id );

		// Now replace meta w/ new updated value array
		$designer_boutique_shop_image_url = get_template_directory_uri().'/assets/images/product/'.$designer_boutique_shop_product_cats.'/' . str_replace(' ', '_', strtolower($designer_boutique_shop_product_title)).'.png';
		$designer_boutique_shop_image_name  = $designer_boutique_shop_product_title.'.png';
		$designer_boutique_shop_upload_dir = wp_upload_dir();
		// Set upload folder
		$designer_boutique_shop_image_data = file_get_contents(esc_url($designer_boutique_shop_image_url));
		// Get image data
		$unique_file_name = wp_unique_filename($designer_boutique_shop_upload_dir['path'], $designer_boutique_shop_image_name);
		// Generate unique name
		$designer_boutique_shop_filename = basename($unique_file_name);
		// Create image file name
		// Check folder permission and define file location
		if (wp_mkdir_p($designer_boutique_shop_upload_dir['path'])) {
		$designer_boutique_shop_file = $designer_boutique_shop_upload_dir['path'].'/'.$designer_boutique_shop_filename;
		} else {
		$designer_boutique_shop_file = $designer_boutique_shop_upload_dir['basedir'].'/'.$designer_boutique_shop_filename;
		}
		
		file_put_contents($designer_boutique_shop_file, $designer_boutique_shop_image_data);
		// Check image file type
		$wp_filetype = wp_check_filetype($designer_boutique_shop_filename, null);
		// Set attachment data
		$attachment = array(
		'post_mime_type' => $wp_filetype['type'],
		'post_title'     => sanitize_file_name($designer_boutique_shop_filename),
		'post_type'      => 'product',
		'post_status'    => 'inherit',
		);

		// Create the attachment
		$designer_boutique_shop_attach_id = wp_insert_attachment($attachment, $designer_boutique_shop_file, $designer_boutique_shop_uqpost_id);

		// Define attachment metadata
		$attach_data = wp_generate_attachment_metadata($designer_boutique_shop_attach_id, $designer_boutique_shop_file);

		// Assign metadata to attachment
		wp_update_attachment_metadata($designer_boutique_shop_attach_id, $attach_data);
		if ( count( $designer_boutique_shop_product_image_gallery ) < 3 ) {
			array_push( $designer_boutique_shop_product_image_gallery, $designer_boutique_shop_attach_id );
		}
		// // And finally assign featured image to post
		set_post_thumbnail($designer_boutique_shop_uqpost_id, $designer_boutique_shop_attach_id);
		++$designer_boutique_shop_n;
		}
		// Create product END
		++$designer_boutique_shop_k;
		}
		// Add Gallery in first simple product and second variable product START
		$designer_boutique_shop_product_image_gallery = implode( ',', $designer_boutique_shop_product_image_gallery );
		foreach ( $designer_boutique_shop_product_ids as $designer_boutique_shop_product_id ) {
		update_post_meta( $designer_boutique_shop_product_id, 'designer_boutique_shop_product_image_gallery', $designer_boutique_shop_product_image_gallery );
		}

	    /* Create Menu */
            $designer_boutique_shop_menuname  = 'Main Menus';
            $designer_boutique_shop_location  = 'main-menu';

            $designer_boutique_shop_menu = wp_get_nav_menu_object( $designer_boutique_shop_menuname );

            if ( ! $designer_boutique_shop_menu ) {
            $designer_boutique_shop_menu_id = wp_create_nav_menu( $designer_boutique_shop_menuname );

           // Home Page 
			wp_update_nav_menu_item( $designer_boutique_shop_menu_id, 0, array(
				'menu-item-title'     => __( 'Home', 'designer-boutique-shop' ),
				'menu-item-url'       => home_url( '/' ),
				'menu-item-type'      => 'custom',
				'menu-item-status'    => 'publish',
				'menu-item-position'  => 1,
			) );

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

			// Pages Page 
			$designer_boutique_shop_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 ( $designer_boutique_shop_about_id ) {
			wp_update_nav_menu_item( $designer_boutique_shop_menu_id, 0, array(
			'menu-item-title'     => 'Pages',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $designer_boutique_shop_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
            'menu-item-position'  => 3,
			) );
			}

            // CREATE SHOP PAGE
                $designer_boutique_shop_about_title = 'Shop';
                $designer_boutique_shop_about_content = 'Lorem ipsum dolor sit amet...';

                $shop_page = get_page_by_path('shop');
                if ( !$shop_page ) {
                    $designer_boutique_shop_about = array(
                        'post_type'   => 'page',
                        'post_title'  => $designer_boutique_shop_about_title,
                        'post_content'=> $designer_boutique_shop_about_content,
                        'post_status' => 'publish',
                        'post_author' => 1,
                        'post_name'   => 'shop' 
                        );
                        $designer_boutique_shop_about_id = wp_insert_post($designer_boutique_shop_about);
                    } else {
                        $designer_boutique_shop_about_id = $shop_page->ID;
                    }

                    wp_update_nav_menu_item($designer_boutique_shop_menu_id, 0, array(
                        'menu-item-title'   => __('Shop', 'designer-boutique-shop'),
                        'menu-item-classes' => 'shop',
                        'menu-item-object-id' => $designer_boutique_shop_about_id,
                        'menu-item-object'  => 'page',
                        'menu-item-type'    => 'post_type',
                        'menu-item-status'  => 'publish'
                    ));
            
			/* ---------- Assign Menu Location ---------- */
			$designer_boutique_shop_locations = get_theme_mod( 'nav_menu_locations', array() );
			$designer_boutique_shop_locations[ $designer_boutique_shop_location ] = $designer_boutique_shop_menu_id;
			set_theme_mod( 'nav_menu_locations', $designer_boutique_shop_locations );
		}
    }
}