<?php
/**
 * EmailClickedRule — customer clicked a link in a prior email from this play.
 *
 * @package CartCue\Rules
 */

namespace CartCue\Rules;

use CartCue\EmailTracker;

defined( 'ABSPATH' ) || exit;

class EmailClickedRule extends AbstractRule {

	public function get_slug(): string {
		return 'email_clicked';
	}

	public function get_label(): string {
		return __( 'Email Clicked (this play)', 'cartcue-sales-automation' );
	}

	public function get_operators(): array {
		return [
			'is'     => __( 'has been clicked', 'cartcue-sales-automation' ),
			'is_not' => __( 'has not been clicked', 'cartcue-sales-automation' ),
		];
	}

	public function get_value_type(): string {
		return 'hidden';
	}

	public function evaluate( array $config, array $data ): bool {
		$workflow_id = (int) ( $data['workflow_id'] ?? 0 );
		$email       = sanitize_email( $data['customer_email'] ?? '' );
		if ( ! $workflow_id || ! is_email( $email ) ) {
			return true;
		}

		$clicked  = EmailTracker::customer_has_clicked( $email, $workflow_id );
		$operator = sanitize_key( $config['operator'] ?? 'is' );

		return 'is_not' === $operator ? ! $clicked : $clicked;
	}
}
