<?php
/**
 * Condition: Guest purchase check.
 *
 * Evaluates whether an order was placed by a guest customer.
 *
 * @package EDD\Pro\Emails\ConditionalTags\Conditions
 * @copyright Copyright (c) 2026, Sandhills Development, LLC
 * @license https://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since 3.6.6
 */

namespace EDD\Pro\Emails\ConditionalTags\Conditions;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

use EDD\Pro\Emails\ConditionalTags\Conditions\Traits\ParseData;

/**
 * Class GuestPurchase
 *
 * @since 3.6.6
 */
class GuestPurchase extends AbstractCondition {
	use ParseData;

	/**
	 * Supported contexts.
	 *
	 * @since 3.6.6
	 * @var string[]
	 */
	protected $supported_contexts = array( 'order' );

	/**
	 * Get the key for this condition.
	 *
	 * @since 3.6.6
	 *
	 * @return string Condition key.
	 */
	public function get_key() {
		return 'is_guest';
	}

	/**
	 * Get the label for this condition.
	 *
	 * @since 3.6.6
	 *
	 * @return string Condition label.
	 */
	public function get_label() {
		return __( 'Guest Purchase', 'easy-digital-downloads' );
	}

	/**
	 * Evaluate whether the order belongs to a guest customer.
	 *
	 * @since 3.6.6
	 *
	 * @param int         $object_id  The EDD object ID.
	 * @param object      $edd_object The EDD object.
	 * @param object      $email      The EDD email object.
	 * @param mixed       $value      Optional expected boolean value.
	 * @param string|null $operator   Optional comparison operator.
	 * @param mixed       $context    Additional contextual data for evaluation.
	 * @return bool
	 */
	public function evaluate( $object_id, $edd_object, $email, $value, $operator = '=', $context = '' ) {
		if ( ! $this->is_context_allowed( $context ) ) {
			return false;
		}

		if ( ! $edd_object instanceof \EDD\Orders\Order ) {
			return false;
		}

		$is_guest = 0 === absint( $edd_object->user_id );

		// Boolean form: {if is_guest}.
		if ( null === $operator && null === $value ) {
			return $is_guest;
		}

		if ( ! in_array( $operator, array( '=', '!=' ), true ) ) {
			return false;
		}

		$expected = $this->parse_boolean( $value );
		if ( null === $expected ) {
			return false;
		}

		return '!=' === $operator ? $is_guest !== $expected : $is_guest === $expected;
	}
}
