<?php
/**
 * OrderTotalRule — passes when an order total meets the configured threshold.
 *
 * @package CartCue\Rules
 */

namespace CartCue\Rules;

defined( 'ABSPATH' ) || exit;

class OrderTotalRule extends AbstractRule {

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

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

	public function get_operators(): array {
		return [
			'greater_than' => __( 'is more than', 'cartcue-sales-automation' ),
			'less_than'    => __( 'is less than', 'cartcue-sales-automation' ),
			'equals'       => __( 'equals', 'cartcue-sales-automation' ),
		];
	}

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

	public function evaluate( array $config, array $data ): bool {
		$order_id = (int) ( $data['order_id'] ?? 0 );
		if ( ! $order_id ) {
			return true; // Rule requires an order; skip gracefully if none.
		}

		$order = wc_get_order( $order_id );
		if ( ! $order ) {
			return false;
		}

		$operator = sanitize_key( $config['operator'] ?? 'greater_than' );
		$expected = (float) ( $config['value'] ?? 0 );
		$actual   = (float) $order->get_total();

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