<?php
/**
 * Demo import for T Shirt Printing Shop.
 *
 * @package T Shirt Printing Shop
 */

add_action( 'wp_ajax_t_shirt_printing_shop_run_demo_import', 't_shirt_printing_shop_run_demo_import' );

/**
 * Import demo products for T Shirt Printing Shop.
 */
function t_shirt_printing_shop_run_demo_import() {
	check_ajax_referer( 't_shirt_printing_shop_demo_nonce', 'nonce' );

	if ( ! current_user_can( 'manage_options' ) ) {
		wp_send_json_error( 'Permission denied' );
	}

	if ( get_option( 't_shirt_printing_shop_demo_import_completed' ) ) {
		wp_send_json_success( 'Already imported' );
	}

	if ( ! class_exists( 'WooCommerce' ) ) {
		wp_send_json_error( 'WooCommerce not active' );
	}

	$t_shirt_printing_shop_categories = array(
		'Round Neck T-Shirts',
		'Polo T-Shirts',
		'Printed Hoodies',
		'Custom Designs',
	);

	$t_shirt_printing_shop_category_ids = array();

	foreach ( $t_shirt_printing_shop_categories as $t_shirt_printing_shop_cat ) {
		$t_shirt_printing_shop_term = term_exists( $t_shirt_printing_shop_cat, 'product_cat' );
		if ( ! $t_shirt_printing_shop_term ) {
			$t_shirt_printing_shop_term = wp_insert_term( $t_shirt_printing_shop_cat, 'product_cat' );
		}

		if ( ! is_wp_error( $t_shirt_printing_shop_term ) ) {
			$t_shirt_printing_shop_category_ids[] = (int) $t_shirt_printing_shop_term['term_id'];
		}
	}

	$t_shirt_printing_shop_products = array(
		array(
			'title'             => 'Classic Logo Tee',
			'price'             => '19.99',
			'image'             => get_template_directory() . '/assets/images/product01.png',
			'short_description' => 'Soft cotton t-shirt with front logo print.',
			'description'       => 'Everyday wear t-shirt made from breathable cotton, designed for comfort and durable print quality.',
		),
		array(
			'title'             => 'Street Art Printed Tee',
			'price'             => '24.99',
			'image'             => get_template_directory() . '/assets/images/product02.png',
			'short_description' => 'Bold graphic print for modern street style.',
			'description'       => 'Vibrant all-day t-shirt with high-quality ink print and fade-resistant colors for long-lasting style.',
		),
		array(
			'title'             => 'Custom Name T-Shirt',
			'price'             => '22.99',
			'image'             => get_template_directory() . '/assets/images/product03.png',
			'short_description' => 'Personalized print t-shirt for gifts and events.',
			'description'       => 'Create your own printed t-shirt with custom text and simple artwork for occasions and team use.',
		),
		array(
			'title'             => 'Premium Polo Print Tee',
			'price'             => '27.99',
			'image'             => get_template_directory() . '/assets/images/product04.png',
			'short_description' => 'Collared polo style with clean chest print.',
			'description'       => 'Smart casual polo t-shirt built with premium fabric and precise printing for a polished branded look.',
		),
	);

	foreach ( $t_shirt_printing_shop_products as $t_shirt_printing_shop_index => $t_shirt_printing_shop_product ) {
		$t_shirt_printing_shop_product_id = wp_insert_post(
			array(
				'post_title'   => $t_shirt_printing_shop_product['title'],
				'post_content' => $t_shirt_printing_shop_product['description'],
				'post_excerpt' => $t_shirt_printing_shop_product['short_description'],
				'post_status'  => 'publish',
				'post_type'    => 'product',
			)
		);

		if ( is_wp_error( $t_shirt_printing_shop_product_id ) ) {
			continue;
		}

		update_post_meta( $t_shirt_printing_shop_product_id, '_regular_price', $t_shirt_printing_shop_product['price'] );
		update_post_meta( $t_shirt_printing_shop_product_id, '_price', $t_shirt_printing_shop_product['price'] );

		if ( ! empty( $t_shirt_printing_shop_category_ids ) ) {
			wp_set_object_terms(
				$t_shirt_printing_shop_product_id,
				$t_shirt_printing_shop_category_ids[ $t_shirt_printing_shop_index % count( $t_shirt_printing_shop_category_ids ) ],
				'product_cat'
			);
		}

		if ( ! empty( $t_shirt_printing_shop_product['image'] ) && file_exists( $t_shirt_printing_shop_product['image'] ) ) {
			if ( ! function_exists( 'media_handle_sideload' ) ) {
				require_once ABSPATH . 'wp-admin/includes/media.php';
				require_once ABSPATH . 'wp-admin/includes/file.php';
				require_once ABSPATH . 'wp-admin/includes/image.php';
			}

			$t_shirt_printing_shop_tmp_file = wp_tempnam( $t_shirt_printing_shop_product['image'] );
			if ( $t_shirt_printing_shop_tmp_file ) {
				copy( $t_shirt_printing_shop_product['image'], $t_shirt_printing_shop_tmp_file );
				$t_shirt_printing_shop_file_array = array(
					'name'     => basename( $t_shirt_printing_shop_product['image'] ),
					'tmp_name' => $t_shirt_printing_shop_tmp_file,
				);

				$t_shirt_printing_shop_attachment_id = media_handle_sideload( $t_shirt_printing_shop_file_array, $t_shirt_printing_shop_product_id );

				if ( is_wp_error( $t_shirt_printing_shop_attachment_id ) ) {
					@unlink( $t_shirt_printing_shop_tmp_file );
				} else {
					set_post_thumbnail( $t_shirt_printing_shop_product_id, $t_shirt_printing_shop_attachment_id );
				}
			}
		}
	}

	update_option( 't_shirt_printing_shop_demo_import_completed', true );
	wp_send_json_success( 'Demo imported successfully' );
}
