<?php
/**
 * Demo import for Architect Designer.
 *
 * @package Architect Designer
 */

add_action( 'wp_ajax_architect_designer_run_demo_import', 'architect_designer_run_demo_import' );

/**
 * Import demo products for Architect Designer.
 */
function architect_designer_run_demo_import() {
	check_ajax_referer( 'architect_designer_demo_nonce', 'nonce' );

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

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

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

	$architect_designer_categories = array(
		'Designer Decor',
		'Modern Sculpture',
		'Interior Accessories',
		'Architect Studio Picks',
	);

	$architect_designer_category_ids = array();

	foreach ( $architect_designer_categories as $architect_designer_cat ) {
		$architect_designer_term = term_exists( $architect_designer_cat, 'product_cat' );
		if ( ! $architect_designer_term ) {
			$architect_designer_term = wp_insert_term( $architect_designer_cat, 'product_cat' );
		}

		if ( ! is_wp_error( $architect_designer_term ) ) {
			$architect_designer_category_ids[] = (int) $architect_designer_term['term_id'];
		}
	}

	$architect_designer_products = array(
		array(
			'title'             => 'Arc Form Table Sculpture',
			'price'             => '159.00',
			'image'             => get_template_directory() . '/assets/images/product01.png',
			'short_description' => 'Contemporary decor piece for refined interiors.',
			'description'       => 'A handcrafted sculptural accent with smooth curves and a gallery-inspired finish for modern living spaces.',
		),
		array(
			'title'             => 'Minimal Stone Accent',
			'price'             => '189.00',
			'image'             => get_template_directory() . '/assets/images/product02.png',
			'short_description' => 'A clean-lined accent that elevates display shelves.',
			'description'       => 'Designed for curated interiors, this decorative object adds balance, texture, and a premium architectural feel.',
		),
		array(
			'title'             => 'Studio Curve Art Object',
			'price'             => '219.00',
			'image'             => get_template_directory() . '/assets/images/product03.png',
			'short_description' => 'Statement decor with soft silhouettes and depth.',
			'description'       => 'An eye-catching centerpiece made to complement reception desks, coffee tables, and designer console settings.',
		),
		array(
			'title'             => 'Heritage Form Decor Piece',
			'price'             => '249.00',
			'image'             => get_template_directory() . '/assets/images/product04.png',
			'short_description' => 'Premium display piece inspired by timeless structure.',
			'description'       => 'Blending artistic geometry with luxury styling, this item brings a sophisticated focal point to any interior concept.',
		),
	);

	foreach ( $architect_designer_products as $architect_designer_index => $architect_designer_product ) {
		$architect_designer_product_id = wp_insert_post(
			array(
				'post_title'   => $architect_designer_product['title'],
				'post_content' => $architect_designer_product['description'],
				'post_excerpt' => $architect_designer_product['short_description'],
				'post_status'  => 'publish',
				'post_type'    => 'product',
			)
		);

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

		update_post_meta( $architect_designer_product_id, '_regular_price', $architect_designer_product['price'] );
		update_post_meta( $architect_designer_product_id, '_price', $architect_designer_product['price'] );

		if ( ! empty( $architect_designer_category_ids ) ) {
			wp_set_object_terms(
				$architect_designer_product_id,
				$architect_designer_category_ids[ $architect_designer_index % count( $architect_designer_category_ids ) ],
				'product_cat'
			);
		}

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

			$architect_designer_tmp_file = wp_tempnam( $architect_designer_product['image'] );
			if ( $architect_designer_tmp_file ) {
				copy( $architect_designer_product['image'], $architect_designer_tmp_file );
				$architect_designer_file_array = array(
					'name'     => basename( $architect_designer_product['image'] ),
					'tmp_name' => $architect_designer_tmp_file,
				);

				$architect_designer_attachment_id = media_handle_sideload( $architect_designer_file_array, $architect_designer_product_id );

				if ( is_wp_error( $architect_designer_attachment_id ) ) {
					@unlink( $architect_designer_tmp_file );
				} else {
					set_post_thumbnail( $architect_designer_product_id, $architect_designer_attachment_id );
				}
			}
		}
	}

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