<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase, WordPress.Files.FileName.InvalidClassFileName
/**
 * WordPress Wholesale Discount Repository implementation.
 *
 * @package Autoship\Repositories\Implementations
 * @since   2.13.0
 */

namespace Autoship\Repositories\Implementations;

use Autoship\Repositories\WholesaleDiscountRepositoryInterface;

/**
 * WordPress implementation of the Wholesale Discount Repository.
 *
 * Reads and writes the `_autoship_schedule_order_wholesale_discount`
 * post meta value using WordPress post meta functions.
 *
 * @package Autoship\Repositories\Implementations
 * @since   2.13.0
 */
class WordPressWholesaleDiscountRepository implements WholesaleDiscountRepositoryInterface {

	/**
	 * The meta key for wholesale discount storage.
	 *
	 * @var string
	 */
	const META_KEY = '_autoship_schedule_order_wholesale_discount';

	/**
	 * Get the wholesale discount for a product.
	 *
	 * @param int $product_id The WooCommerce product ID.
	 *
	 * @return float|null The discount value, or null if not set.
	 */
	public function get_discount( int $product_id ): ?float {
		$value = get_post_meta( $product_id, self::META_KEY, true );

		if ( '' === $value || false === $value ) {
			return null;
		}

		return (float) $value;
	}

	/**
	 * Set the wholesale discount for a product.
	 *
	 * @param int   $product_id The WooCommerce product ID.
	 * @param float $value      The discount value.
	 *
	 * @return bool True on success, false on failure.
	 */
	public function set_discount( int $product_id, float $value ): bool {
		$result = update_post_meta( $product_id, self::META_KEY, $value );

		return false !== $result;
	}
}
