<?php
/**
 * Date query rules helper.
 *
 * @package PRECO
 */

namespace PRECO\Includes\Frontend\Engine\Filters\Query;

defined( 'ABSPATH' ) || exit;

/**
 * Date query rules helper.
 */
class DateRuleHelper {
	/**
	 * Add a "created after" date query clause based on a relative time window.
	 *
	 * @param array $date_query Date query array, modified by reference.
	 * @param mixed $value      Relative window key (e.g. 'last_7d').
	 * @return void
	 */
	public function add_created_rule( &$date_query, $value ) {
		$after = null;
		switch ( (string) $value ) {
			case 'last_24h':
				$after = \gmdate( 'Y-m-d H:i:s', time() - DAY_IN_SECONDS );
				break;
			case 'last_7d':
				$after = \gmdate( 'Y-m-d H:i:s', time() - 7 * DAY_IN_SECONDS );
				break;
			case 'last_30d':
				$after = \gmdate( 'Y-m-d H:i:s', time() - 30 * DAY_IN_SECONDS );
				break;
			case 'last_3m':
				$after = \gmdate( 'Y-m-d H:i:s', time() - 90 * DAY_IN_SECONDS );
				break;
			default:
				$after = null;
		}

		if ( $after ) {
			$date_query[] = array(
				'after'     => $after,
				'inclusive' => true,
				'column'    => 'post_date_gmt',
			);
		}
	}
}
