<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase, WordPress.Files.FileName.InvalidClassFileName
/**
 * Snooze-aware QPilot feature flag checker.
 *
 * @package Autoship\Services\Snooze\Implementations
 * @since   2.13.0
 */

namespace Autoship\Services\Snooze\Implementations;

use Autoship\Core\Plugin;
use Autoship\Services\Snooze\Interfaces\SnoozeFeatureFlagsInterface;

/**
 * Reads the QPilot remote feature flag list (merged global + site) from
 * `Plugin::fetch_qpilot_feature_flags()` and answers Snooze-specific
 * questions about it.
 */
class SnoozeFeatureFlags implements SnoozeFeatureFlagsInterface {

	/**
	 * The flag name we care about.
	 */
	private const FLAG_NAME = 'enable-snooze';

	/**
	 * @inheritDoc
	 */
	public function is_snooze_flag_enabled(): bool {
		$flags = $this->get_flags();

		// Permissive: if flags couldn't be fetched, don't block the merchant.
		if ( empty( $flags ) ) {
			return (bool) apply_filters( 'autoship_is_qpilot_snooze_flag_enabled', true, $flags );
		}

		$enabled = in_array( self::FLAG_NAME, $flags, true );

		return (bool) apply_filters( 'autoship_is_qpilot_snooze_flag_enabled', $enabled, $flags );
	}

	/**
	 * @inheritDoc
	 */
	public function is_snooze_flag_definitively_off(): bool {
		$flags = $this->get_flags();

		if ( empty( $flags ) ) {
			return false;
		}

		return ! in_array( self::FLAG_NAME, $flags, true );
	}

	/**
	 * @inheritDoc
	 */
	public function get_flags(): array {
		$plugin = Plugin::get_instance();

		if ( null === $plugin ) {
			return array();
		}

		$flags = $plugin->fetch_qpilot_feature_flags();

		return is_array( $flags ) ? $flags : array();
	}
}
