<?php

declare(strict_types=1);

namespace Drupal\native_observability_dashboard\Service;

use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\native_observability_dashboard\ValueObject\TimeRange;

/**
 * Builds the Cache tab payload for the dashboard.
 */
final readonly class DashboardCacheBuilder {

  /**
   * Creates a new cache dashboard builder instance.
   *
   * @param \Drupal\native_observability_dashboard\Service\CacheObservabilitySectionBuilder $cacheObservabilitySectionBuilder
   *   The cache observability section builder.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter
   *   The date formatter service.
   */
  public function __construct(
    private CacheObservabilitySectionBuilder $cacheObservabilitySectionBuilder,
    private DateFormatterInterface $dateFormatter,
  ) {
  }

  /**
   * Builds the Cache dashboard payload.
   *
   * @param \Drupal\native_observability_dashboard\ValueObject\TimeRange|null $range
   *   The selected dashboard time range.
   *
   * @return array
   *   The complete cache tab payload.
   */
  public function build(?TimeRange $range = NULL): array {
    return [
      'generated_at' => $this->dateFormatter->format(time(), 'custom', 'Y-m-d H:i:s'),
      'sections' => [
        'cache_observability' => $this->cacheObservabilitySectionBuilder->build($range),
      ],
    ];
  }

}
