<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase, WordPress.Files.FileName.InvalidClassFileName
/**
 * Value object for wholesale item data.
 *
 * @package Autoship\Domain\WholesalePricing
 * @since   2.13.0
 */

namespace Autoship\Domain\WholesalePricing;

/**
 * Immutable value object representing a wholesale pricing item.
 *
 * Replaces the duplicated $autoship associative array construction
 * pattern used across multiple methods in the wholesale pricing class.
 *
 * @package Autoship\Domain\WholesalePricing
 * @since   2.13.0
 */
final class WholesaleItemData {

	/**
	 * The product ID.
	 *
	 * @var int
	 */
	private int $product_id;

	/**
	 * The item price.
	 *
	 * @var float
	 */
	private float $price;

	/**
	 * The item quantity.
	 *
	 * @var int
	 */
	private int $qty;

	/**
	 * The product type: 'simple' or 'variation'.
	 *
	 * @var string
	 */
	private string $type;

	/**
	 * The rule ID (product ID for simple, parent ID for variations).
	 *
	 * @var int
	 */
	private int $rule_id;

	/**
	 * Constructor.
	 *
	 * @param int    $product_id The product ID.
	 * @param float  $price      The item price.
	 * @param int    $qty        The item quantity.
	 * @param string $type       The product type ('simple' or 'variation').
	 * @param int    $rule_id    The rule ID.
	 */
	public function __construct( int $product_id, float $price, int $qty, string $type, int $rule_id ) {
		$this->product_id = $product_id;
		$this->price      = $price;
		$this->qty        = $qty;
		$this->type       = $type;
		$this->rule_id    = $rule_id;
	}

	/**
	 * Create from a WooCommerce product.
	 *
	 * @param \WC_Product $product The WooCommerce product.
	 * @param float       $price   The item price.
	 * @param int         $qty     The item quantity.
	 *
	 * @return self
	 */
	public static function from_wc_product( \WC_Product $product, float $price, int $qty ): self {
		$type    = $product->is_type( 'simple' ) ? 'simple' : 'variation';
		$rule_id = 'simple' === $type ? $product->get_id() : wp_get_post_parent_id( $product->get_id() );

		return new self( $product->get_id(), $price, $qty, $type, $rule_id );
	}

	/**
	 * Get the product ID.
	 *
	 * @return int
	 */
	public function get_product_id(): int {
		return $this->product_id;
	}

	/**
	 * Get the item price.
	 *
	 * @return float
	 */
	public function get_price(): float {
		return $this->price;
	}

	/**
	 * Get the item quantity.
	 *
	 * @return int
	 */
	public function get_qty(): int {
		return $this->qty;
	}

	/**
	 * Get the product type.
	 *
	 * @return string
	 */
	public function get_type(): string {
		return $this->type;
	}

	/**
	 * Get the rule ID.
	 *
	 * @return int
	 */
	public function get_rule_id(): int {
		return $this->rule_id;
	}

	/**
	 * Convert to the legacy associative array format.
	 *
	 * @return array
	 */
	public function to_array(): array {
		return array(
			'id'      => $this->product_id,
			'price'   => $this->price,
			'qty'     => $this->qty,
			'type'    => $this->type,
			'rule_id' => $this->rule_id,
		);
	}
}
