<?php
/**
 * Demo import for Skincare Product Store.
 *
 * @package Skincare Product Store
 */

add_action( 'wp_ajax_skincare_product_store_run_demo_import', 'skincare_product_store_run_demo_import' );

/**
 * Import demo products for Skincare Product Store.
 */
function skincare_product_store_run_demo_import() {
	check_ajax_referer( 'skincare_product_store_demo_nonce', 'nonce' );

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

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

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

	$skincare_product_store_categories = array(
		'Cleansers',
		'Serums',
		'Moisturizers',
		'Sun Care',
	);

	$skincare_product_store_category_ids = array();

	foreach ( $skincare_product_store_categories as $skincare_product_store_cat ) {
		$skincare_product_store_term = term_exists( $skincare_product_store_cat, 'product_cat' );
		if ( ! $skincare_product_store_term ) {
			$skincare_product_store_term = wp_insert_term( $skincare_product_store_cat, 'product_cat' );
		}

		if ( ! is_wp_error( $skincare_product_store_term ) ) {
			$skincare_product_store_category_ids[] = (int) $skincare_product_store_term['term_id'];
		}
	}

	$skincare_product_store_products = array(
		array(
			'title'             => 'Radiance Clean Gel',
			'price'             => '24.99',
			'image'             => get_template_directory() . '/assets/images/product01.png',
			'short_description' => 'Gentle daily cleanser for fresh and balanced skin.',
			'description'       => 'A lightweight cleansing gel that removes excess oil and impurities while keeping the skin soft, calm, and hydrated.',
		),
		array(
			'title'             => 'Glow Repair Serum',
			'price'             => '34.99',
			'image'             => get_template_directory() . '/assets/images/product02.png',
			'short_description' => 'Brightening serum designed for dull and tired skin.',
			'description'       => 'This fast-absorbing serum helps improve skin texture, supports an even-looking tone, and adds a healthy natural glow.',
		),
		array(
			'title'             => 'Hydra Soft Cream',
			'price'             => '29.99',
			'image'             => get_template_directory() . '/assets/images/product03.png',
			'short_description' => 'Rich moisturizer for all-day nourishment.',
			'description'       => 'A replenishing face cream made to lock in moisture, smooth dry patches, and leave the skin feeling supple throughout the day.',
		),
		array(
			'title'             => 'Daily Defense SPF',
			'price'             => '27.99',
			'image'             => get_template_directory() . '/assets/images/product04.png',
			'short_description' => 'Everyday sun care with a smooth, non-greasy finish.',
			'description'       => 'A lightweight SPF formula that layers comfortably under makeup while helping protect the skin during your daily routine.',
		),
	);

	foreach ( $skincare_product_store_products as $skincare_product_store_index => $skincare_product_store_product ) {
		$skincare_product_store_product_id = wp_insert_post(
			array(
				'post_title'   => $skincare_product_store_product['title'],
				'post_content' => $skincare_product_store_product['description'],
				'post_excerpt' => $skincare_product_store_product['short_description'],
				'post_status'  => 'publish',
				'post_type'    => 'product',
			)
		);

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

		update_post_meta( $skincare_product_store_product_id, '_regular_price', $skincare_product_store_product['price'] );
		update_post_meta( $skincare_product_store_product_id, '_price', $skincare_product_store_product['price'] );

		if ( ! empty( $skincare_product_store_category_ids ) ) {
			wp_set_object_terms(
				$skincare_product_store_product_id,
				$skincare_product_store_category_ids[ $skincare_product_store_index % count( $skincare_product_store_category_ids ) ],
				'product_cat'
			);
		}

		if ( ! empty( $skincare_product_store_product['image'] ) && file_exists( $skincare_product_store_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';
			}

			$skincare_product_store_tmp_file = wp_tempnam( $skincare_product_store_product['image'] );
			if ( $skincare_product_store_tmp_file ) {
				copy( $skincare_product_store_product['image'], $skincare_product_store_tmp_file );
				$skincare_product_store_file_array = array(
					'name'     => basename( $skincare_product_store_product['image'] ),
					'tmp_name' => $skincare_product_store_tmp_file,
				);

				$skincare_product_store_attachment_id = media_handle_sideload( $skincare_product_store_file_array, $skincare_product_store_product_id );

				if ( is_wp_error( $skincare_product_store_attachment_id ) ) {
					@unlink( $skincare_product_store_tmp_file );
				} else {
					set_post_thumbnail( $skincare_product_store_product_id, $skincare_product_store_attachment_id );
				}
			}
		}
	}

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