<?php
/**
 * Omnibus by Pietrzak provider
 *
 * @package Ilabs\Firevert\Core\Integration\Omnibus
 */

declare( strict_types=1 );

namespace Ilabs\Firevert\Core\Integration\Omnibus;

/**
 * Omnibus by Pietrzak integration provider.
 */
final class OmnibusByPietrzakProvider extends AbstractOmnibusProvider {

	/**
	 * Returns the readable name of the source plugin.
	 *
	 * @return string Plugin name.
	 */
	public function get_plugin_name(): string {
		return 'Omnibus — show the lowest price';
	}

	/**
	 * Returns the readable name of the source plugin creator.
	 *
	 * @return string Plugin creator.
	 */
	public function get_plugin_creator(): string {
		return 'Marcin Pietrzak';
	}

	/**
	 * Checks if the Omnibus plugin is active.
	 *
	 * @return bool True if a plugin is active.
	 */
	public function is_active(): bool {
		return is_plugin_active( 'omnibus/omnibus.php' );
	}

	/**
	 * Retrieves the lowest price from the post meta.
	 *
	 * @param int $product_id WooCommerce product ID.
	 *
	 * @return float|null Lowest price or null.
	 */
	public function get_lowest_price( int $product_id ): ?float {
		$lowest = apply_filters( 'iworks_omnibus_wc_get_lowest_price', array(), $product_id );

		if ( is_numeric( $lowest ) ) {
			return (float) $lowest;
		}

		return null;
	}

	/**
	 * Retrieves the date of the lowest price from post meta.
	 *
	 * @param int $product_id WooCommerce product ID.
	 * @return string|null Date in 'Y-m-d' format or null.
	 */
	public function get_lowest_price_date( int $product_id ): ?string {
		$timestamp = get_post_meta( $product_id, '_iwo_price_lowest_date', true );

		if ( is_numeric( $timestamp ) && $timestamp > 0 ) {
			return gmdate( 'Y-m-d', (int) $timestamp );
		}

		return null;
	}
}
