<?php
/**
 * OrderCurrencyRule — passes when an order currency matches the configured value.
 *
 * @package CartCue\Rules
 */

namespace CartCue\Rules;

defined( 'ABSPATH' ) || exit;

class OrderCurrencyRule extends AbstractRule {

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

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

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

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

	public function get_value_options(): array {
		$options = [];
		foreach ( get_woocommerce_currencies() as $code => $label ) {
			$options[ $code ] = $label . ' (' . $code . ')';
		}
		return $options;
	}

	public function evaluate( array $config, array $data ): bool {
		$order_id = (int) ( $data['order_id'] ?? 0 );
		if ( ! $order_id ) {
			return true;
		}

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

		$operator = sanitize_key( $config['operator'] ?? 'equals' );
		$expected = sanitize_text_field( $config['value'] ?? '' );
		$actual   = $order->get_currency();

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