<?php

declare(strict_types=1);

namespace Drupal\Tests\native_observability_report\Unit\Service;

use Drupal\Tests\UnitTestCase;
use Drupal\native_observability_report\Service\ObservabilityScoringService;

/**
 * Covers the normalized scoring rules used by the report layer.
 */
final class ObservabilityScoringServiceTest extends UnitTestCase {

  /**
   * Verifies the overview score for a healthy payload.
   */
  public function testScoreOverviewReturnsGoodStatus(): void {
    $service = new ObservabilityScoringService();

    $result = $service->scoreOverview([
      'sections' => [
        'request_timeline' => [
          'summary' => ['slow_request_rate' => 4.5],
        ],
        'technical_insights' => [
          'items' => ['a'],
        ],
      ],
    ]);

    $this->assertSame('good', $result['status']);
    $this->assertSame(92, $result['score']);
    $this->assertSame(1, $result['severity']);
  }

  /**
   * Verifies the overview score for a warning scenario.
   */
  public function testScoreOverviewReturnsWarningStatus(): void {
    $service = new ObservabilityScoringService();

    $result = $service->scoreOverview([
      'sections' => [
        'request_timeline' => [
          'summary' => ['slow_request_rate' => 12.0],
        ],
        'technical_insights' => [
          'items' => ['a'],
        ],
      ],
    ]);

    $this->assertSame('warning', $result['status']);
    $this->assertSame(68, $result['score']);
    $this->assertSame(2, $result['severity']);
  }

  /**
   * Verifies the cache score in a production-critical scenario.
   */
  public function testScoreCacheReturnsCriticalStatusInProductionWithoutDynamicCache(): void {
    $service = new ObservabilityScoringService();

    $result = $service->scoreCache([
      'sections' => [
        'cache_observability' => [
          'summary' => [
            'dynamic_page_cache_enabled' => FALSE,
            'dynamic_cache_hit_rate' => 0.0,
          ],
        ],
      ],
    ], TRUE);

    $this->assertSame('critical', $result['status']);
    $this->assertSame(25, $result['score']);
    $this->assertSame(3, $result['severity']);
  }

  /**
   * Verifies the database score in a warning scenario.
   */
  public function testScoreDatabaseReturnsWarningStatus(): void {
    $service = new ObservabilityScoringService();

    $result = $service->scoreDatabase([
      'sections' => [
        'database_observability' => [
          'summary' => [
            'average_db_time_ms' => 55.0,
            'slow_query_incidence' => 4.0,
          ],
        ],
      ],
    ]);

    $this->assertSame('warning', $result['status']);
    $this->assertSame(66, $result['score']);
    $this->assertSame(2, $result['severity']);
  }

}
