<?php

declare(strict_types=1);

namespace Drupal\Tests\native_observability_eca_bridge\Unit\Plugin\ECA\Event;

use Drupal\native_observability\Event\NativeObservabilityEvents;
use Drupal\native_observability\Event\TraceRecordedEvent;
use Drupal\native_observability\Event\TracesDeletedEvent;
use Drupal\native_observability_eca_bridge\Eca\NativeObservabilityEventDefinitions;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;

/**
 * Unit tests for Native Observability ECA event metadata.
 *
 * Covers definitions() structure without loading the ECA plugin base class.
 * Slow-request filtering is expressed as an ECA condition on duration_ms, not
 * as a separate plugin derivative.
 */
#[CoversClass(NativeObservabilityEventDefinitions::class)]
#[Group('native_observability_eca_bridge')]
class NativeObservabilityEventTest extends UnitTestCase {

  /**
   * Tests that definitions() exposes exactly two variants.
   */
  public function testDefinitionsContainsTwoVariants(): void {
    $definitions = NativeObservabilityEventDefinitions::definitions();

    $this->assertCount(2, $definitions);
    $this->assertArrayHasKey('trace_recorded', $definitions);
    $this->assertArrayHasKey('traces_deleted', $definitions);
  }

  /**
   * Tests that trace_recorded maps to the correct Symfony event and class.
   */
  public function testTraceRecordedDefinitionUsesCorrectEventClass(): void {
    $definitions = NativeObservabilityEventDefinitions::definitions();

    $this->assertSame(TraceRecordedEvent::class, $definitions['trace_recorded']['event_class']);
    $this->assertSame(NativeObservabilityEvents::TRACE_RECORDED, $definitions['trace_recorded']['event_name']);
  }

  /**
   * Tests that traces_deleted maps to the correct Symfony event and class.
   */
  public function testTracesDeletedDefinitionUsesCorrectEventClass(): void {
    $definitions = NativeObservabilityEventDefinitions::definitions();

    $this->assertSame(TracesDeletedEvent::class, $definitions['traces_deleted']['event_class']);
    $this->assertSame(NativeObservabilityEvents::TRACES_DELETED, $definitions['traces_deleted']['event_name']);
  }

  /**
   * Tests that no two definitions share the same Symfony event name.
   *
   * ECA throws InvalidPluginDefinitionException when two plugins map to the
   * same system event, so uniqueness of event_name is a hard requirement.
   */
  public function testAllDefinitionsHaveUniqueEventNames(): void {
    $definitions = NativeObservabilityEventDefinitions::definitions();
    $eventNames = array_column($definitions, 'event_name');

    $this->assertSame(array_unique($eventNames), $eventNames);
  }

}
