<?php
/**
 * CustomerEmailEngagementRule — email open dormancy for branches and campaigns.
 *
 * @package CartCue\Rules
 */

namespace CartCue\Rules;

use CartCue\EmailTracker;

defined( 'ABSPATH' ) || exit;

class CustomerEmailEngagementRule extends AbstractRule {

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

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

	public function get_operators(): array {
		return [
			'opened_within'     => __( 'opened a CartCue email within (days)', 'cartcue-sales-automation' ),
			'not_opened_within' => __( 'has NOT opened a CartCue email within (days)', 'cartcue-sales-automation' ),
		];
	}

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

	public function evaluate( array $config, array $data ): bool {
		$email = sanitize_email( $data['customer_email'] ?? '' );
		if ( ! is_email( $email ) ) {
			return true;
		}
		$days     = max( 1, (int) ( $config['value'] ?? 90 ) );
		$operator = sanitize_key( $config['operator'] ?? 'not_opened_within' );
		$opened   = EmailTracker::customer_opened_within_days( $email, $days );

		return 'opened_within' === $operator ? $opened : ! $opened;
	}
}
