<?php
/**
 * OrderStatusRule — current order status (slug without wc- prefix).
 *
 * @package CartCue\Rules
 */

namespace CartCue\Rules;

defined( 'ABSPATH' ) || exit;

class OrderStatusRule extends AbstractRule {

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

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

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

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

	public function get_value_options(): array {
		$opts = [];
		foreach ( wc_get_order_statuses() as $slug => $label ) {
			$key          = preg_replace( '/^wc-/', '', (string) $slug );
			$opts[ $key ] = $label;
		}
		return $opts;
	}

	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;
		}

		$current = preg_replace( '/^wc-/', '', (string) $order->get_status() );
		$expect  = sanitize_key( $config['value'] ?? '' );
		$op      = $config['operator'] ?? 'equals';

		if ( 'is_not' === $op ) {
			return $current !== $expect;
		}
		return $current === $expect;
	}
}
