<?php

declare(strict_types=1);

namespace Drupal\Tests\native_observability\Unit\Service;

use Drupal\Tests\UnitTestCase;
use Drupal\native_observability\Service\RequestLogExclusionRuleNormalizer;
use PHPUnit\Framework\Attributes\CoversClass;

/**
 * Tests request-log exclusion rule normalization.
 */
#[CoversClass(RequestLogExclusionRuleNormalizer::class)]
final class RequestLogExclusionRuleNormalizerTest extends UnitTestCase {

  /**
   * Tests textarea parsing for query conditions.
   */
  public function testParseConditionDefinition(): void {
    $normalizer = new RequestLogExclusionRuleNormalizer();

    $conditions = $normalizer->parseConditionDefinition(implode("\n", [
      'from|between|2026-01-01|2026-01-31',
      'preview|equals|1',
      'token|exists',
    ]));

    $this->assertCount(3, $conditions);
    $this->assertSame('from', $conditions[0]['key']);
    $this->assertSame('between', $conditions[0]['operator']);
    $this->assertSame('2026-01-31', $conditions[0]['value_to']);
    $this->assertSame('exists', $conditions[2]['operator']);
  }

  /**
   * Tests encode/decode roundtrip for conditions.
   */
  public function testEncodeDecodeRoundTrip(): void {
    $normalizer = new RequestLogExclusionRuleNormalizer();

    $conditions = [
      ['key' => 'from', 'operator' => 'gte', 'value' => '10'],
      ['key' => 'to', 'operator' => 'lte', 'value' => '20'],
    ];

    $encoded = $normalizer->encodeConditions($conditions);
    $decoded = $normalizer->decodeConditions($encoded);

    $this->assertSame('from', $decoded[0]['key']);
    $this->assertSame('gte', $decoded[0]['operator']);
    $this->assertSame('20', $decoded[1]['value']);
  }

}
