<?php

declare(strict_types=1);

namespace Drupal\Tests\native_observability_dashboard\Unit\Service;

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\native_observability_dashboard\Service\DashboardTimeRangeResolver;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Covers request-driven dashboard range resolution.
 */
final class DashboardTimeRangeResolverTest extends UnitTestCase {

  /**
   * Verifies that an allowed query value is converted into a time range.
   */
  public function testResolveUsesAllowedRangeFromRequest(): void {
    $requestStack = new RequestStack();
    $requestStack->push(new Request(['range' => '1h']));

    $resolver = new DashboardTimeRangeResolver(
      $requestStack,
      $this->createMock(RouteMatchInterface::class),
    );

    $range = $resolver->resolve();

    $this->assertSame('1h', $range->getKey());
    $this->assertSame('Last 1 hour', $range->getLabel());
    $this->assertGreaterThanOrEqual(3599, $range->getDurationInSeconds());
    $this->assertLessThanOrEqual(3600, $range->getDurationInSeconds());
  }

  /**
   * Verifies that an unsupported query value falls back to the default range.
   */
  public function testResolveFallsBackToDefaultRange(): void {
    $requestStack = new RequestStack();
    $requestStack->push(new Request(['range' => 'invalid']));

    $resolver = new DashboardTimeRangeResolver(
      $requestStack,
      $this->createMock(RouteMatchInterface::class),
    );

    $range = $resolver->resolve();

    $this->assertSame('30m', $range->getKey());
    $this->assertSame('Last 30 minutes', $range->getLabel());
    $this->assertGreaterThanOrEqual(1799, $range->getDurationInSeconds());
    $this->assertLessThanOrEqual(1800, $range->getDurationInSeconds());
  }

}
