<?php
/**
 * Loads a catalog fixture (tests/fixtures/catalog.json's shape) as a CatalogRepositoryInterface.
 *
 * @package EmberPrice
 */

namespace EmberPrice\Matching;

/**
 * Production counterpart of the test-only fixture loader used throughout
 * the Matching test suites: reads a JSON file shaped like
 * `{ "products": [ {product_id, sku, gtin, brand, title, variations?}, ... ] }`
 * and serves it to CandidateIndex. Used by Rest\RegressionCommand (T-208)
 * to run the match cascade against the synthetic catalog fixture instead
 * of a real WooCommerce store. Pure PHP, no WordPress dependency.
 */
final class JsonCatalogRepository implements CatalogRepositoryInterface {

	/**
	 * Path to the catalog JSON file.
	 *
	 * @var string
	 */
	private string $path;

	/**
	 * Constructor.
	 *
	 * @param string $path Path to the catalog JSON file.
	 */
	public function __construct( string $path ) {
		$this->path = $path;
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_products(): iterable {
		$content = (string) file_get_contents( $this->path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- deliberately WordPress-free; reads a local JSON fixture, not a remote URL.
		$decoded = json_decode( $content, true );

		foreach ( (array) ( $decoded['products'] ?? array() ) as $product ) {
			yield $product;
		}
	}
}
