<?php
/**
 * CustomerTotalSpentRule — passes based on the customer's lifetime spend.
 *
 * @package CartCue\Rules
 */

namespace CartCue\Rules;

defined( 'ABSPATH' ) || exit;

class CustomerTotalSpentRule extends AbstractRule {

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

	public function get_label(): string {
		return __( 'Customer Lifetime Spend', 'cartcue-sales-automation' );
	}

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

	public function evaluate( array $config, array $data ): bool {
		$customer_id = (int) ( $data['customer_id'] ?? 0 );

		if ( ! $customer_id && ! empty( $data['order_id'] ) ) {
			$order       = wc_get_order( (int) $data['order_id'] );
			$customer_id = $order ? (int) $order->get_customer_id() : 0;
		}

		if ( ! $customer_id ) {
			return false;
		}

		$spent = (float) wc_get_customer_total_spent( $customer_id );

		$operator = sanitize_key( $config['operator'] ?? 'greater_than' );
		$expected = (float) ( $config['value'] ?? 0 );

		return $this->compare( $spent, $operator, $expected );
	}
}
