<?php

namespace Dobare\Abandoned_Cart;

use Dobare\Utils\Dobare_Utils;

class Dobare_Abandoned_Cart_Guard
{
	public const OPTION_ENABLED = 'abandoned_cart_enabled';
	public const OPTION_DELAY_MINUTES = 'abandoned_cart_delay_minutes';
	private const DEFAULT_DELAY = 30;
	private const MIN_DELAY = 20;

	public static function is_feature_enabled(): bool
	{
		return (bool) Dobare_Utils::dob_get_option(self::OPTION_ENABLED, false);
	}

	public static function get_delay_minutes(): int
	{
		$delay = (int) Dobare_Utils::dob_get_option(self::OPTION_DELAY_MINUTES, self::DEFAULT_DELAY);

		return max(self::MIN_DELAY, $delay);
	}

	public static function get_min_delay_minutes(): int
	{
		return self::MIN_DELAY;
	}

	public static function has_valid_session_phone(): bool
	{
		return Dobare_Utils::has_valid_phone_for_abandoned_cart();
	}

	public static function current_phone(): ?string
	{
		return Dobare_Utils::get_abandoned_cart_phone();
	}

	public static function is_operational(): bool
	{
		if (! self::is_feature_enabled()) {
			return false;
		}

		return self::has_valid_session_phone();
	}

	public static function seed_defaults(): void
	{
		if (! self::option_exists(self::OPTION_ENABLED)) {
			Dobare_Utils::dob_update_option(self::OPTION_ENABLED, false);
		}

		if (! self::option_exists(self::OPTION_DELAY_MINUTES)) {
			Dobare_Utils::dob_update_option(self::OPTION_DELAY_MINUTES, self::DEFAULT_DELAY);
		}
	}

	private static function option_exists(string $key): bool
	{
		return get_option('DOBARE-' . $key, null) !== null;
	}
}


