<?php

declare(strict_types=1);

namespace Drupal\Tests\native_observability_report\Unit\Service;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\native_observability_report\Service\ObservabilityImpactEstimator;

/**
 * Covers the overhead estimation logic for enabled observability features.
 */
final class ObservabilityImpactEstimatorTest extends UnitTestCase {

  /**
   * Verifies that enabled modules contribute to the computed overhead.
   */
  public function testEstimateBuildsHighImpactPayload(): void {
    $moduleHandler = $this->createMock(ModuleHandlerInterface::class);
    $moduleHandler->method('moduleExists')
      ->willReturnMap([
        ['native_observability_spans', TRUE],
        ['native_observability_cache_observer', TRUE],
        ['native_observability_database_observer', TRUE],
        ['native_observability_logging', FALSE],
      ]);

    $estimator = new ObservabilityImpactEstimator(
      $moduleHandler,
      $this->createMock(ConfigFactoryInterface::class),
    );

    $result = $estimator->estimate();

    $this->assertSame(10.0, $result['estimated_overhead_percent']);
    $this->assertSame('high', $result['impact_level']);
    $this->assertSame([
      'Trace and span collection',
      'Cache response and invalidation observation',
      'Database query observation',
    ], $result['contributors']);
  }

  /**
   * Verifies that a missing feature set produces a low impact payload.
   */
  public function testEstimateBuildsLowImpactPayloadWhenNothingIsEnabled(): void {
    $moduleHandler = $this->createMock(ModuleHandlerInterface::class);
    $moduleHandler->method('moduleExists')->willReturn(FALSE);

    $estimator = new ObservabilityImpactEstimator(
      $moduleHandler,
      $this->createMock(ConfigFactoryInterface::class),
    );

    $result = $estimator->estimate();

    $this->assertSame(0.0, $result['estimated_overhead_percent']);
    $this->assertSame('low', $result['impact_level']);
    $this->assertSame([], $result['contributors']);
  }

}
