<?php
/**
 * In-memory placeholder product for the admin builder's live iframe.
 *
 * @package PRECO
 */

namespace PRECO\Includes\Frontend\Core;

defined( 'ABSPATH' ) || exit;

/**
 * Stands in for a real product in preview-mode recommendation cards, so the
 * builder's live iframe never surfaces actual store products, prices or
 * photos to the person configuring recommendation rules.
 *
 * Extends WC_Product_Simple (constructed with ID 0, so nothing is read from
 * the database) purely so the shared RecommendationsRenderer/AbstractLayout
 * rendering pipeline keeps producing the same markup as a real product card
 * (see PreviewRenderer's docblock) — every getter that would otherwise hit
 * the database is overridden below to return mock data instead.
 */
class DummyProduct extends \WC_Product_Simple {

	/**
	 * Fixed mock categories, mirroring the builder's non-iframe settings
	 * preview (src/.../recommended-products/layouts/index.js).
	 *
	 * @var string[]
	 */
	private $dummy_categories;

	/**
	 * Constructor.
	 *
	 * @param int $index Zero-based position within the current sample pool —
	 *                    drives the mock title and a deterministic rating.
	 */
	public function __construct( $index ) {
		parent::__construct( 0 );

		$index = max( 0, (int) $index );

		// Astronomically unlikely to collide with a real post ID, so
		// get_the_terms() lookups elsewhere never accidentally attach a
		// real store category to this placeholder.
		$this->set_id( PHP_INT_MAX - $index );

		$this->set_name(
			sprintf(
				/* translators: %d: mock product number */
				__( 'Product Title %d', 'wow-ai-product-recommendations' ),
				$index + 1
			)
		);
		$this->set_regular_price( '24.00' );
		$this->set_price( '24.00' );
		$this->set_tax_status( 'none' );
		// Same spread as the JS mock's getMockAverageRating(): 1–5 in 0.5 steps.
		$this->set_average_rating( ( $index % 9 ) / 2 + 1 );

		$this->dummy_categories = array(
			__( 'Cloths', 'wow-ai-product-recommendations' ),
			__( 'Men', 'wow-ai-product-recommendations' ),
		);
	}

	/**
	 * Category labels for AbstractLayout::render_category() to use instead
	 * of a get_the_terms() lookup, which would find nothing for this
	 * placeholder's non-existent post ID.
	 *
	 * @return string[]
	 */
	public function get_preco_dummy_categories() {
		return $this->dummy_categories;
	}

	/**
	 * Product type.
	 *
	 * @return string
	 */
	public function get_type() {
		return 'simple';
	}

	/**
	 * Always purchasable/in-stock so the shared renderer's validity filters
	 * (and the "Add to cart" element) treat this like a normal product.
	 *
	 * @return bool
	 */
	public function is_purchasable() {
		return true;
	}

	/**
	 * Always in stock for preview purposes.
	 *
	 * @return bool
	 */
	public function is_in_stock() {
		return true;
	}

	/**
	 * No real page to link to.
	 *
	 * @return string
	 */
	public function get_permalink() {
		return '#';
	}

	/**
	 * No real product to add to cart.
	 *
	 * @return string
	 */
	public function add_to_cart_url() {
		return '#';
	}

	/**
	 * Add-to-cart button label.
	 *
	 * @return string
	 */
	public function add_to_cart_text() {
		return __( 'Add to cart', 'wow-ai-product-recommendations' );
	}

	/**
	 * Always the builder's placeholder artwork, never a real product photo.
	 *
	 * @param string $size Image size (unused beyond the class name it produces).
	 * @param array  $attr Unused, kept for signature parity with WC_Product::get_image().
	 * @param bool   $placeholder Unused, kept for signature parity with WC_Product::get_image().
	 * @return string
	 */
	public function get_image( $size = 'woocommerce_thumbnail', $attr = array(), $placeholder = true ) {
		$size = is_string( $size ) ? $size : 'woocommerce_thumbnail';

		return sprintf(
			'<img src="%1$s" alt="%2$s" class="attachment-%3$s size-%3$s wp-post-image" />',
			esc_url( PRECO_URL . 'assets/img/builder/product.png' ),
			esc_attr( $this->get_name() ),
			esc_attr( $size )
		);
	}
}
