<?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 Database tab payload for the dashboard.
 */
final readonly class DashboardDatabaseBuilder {

  /**
   * Creates a new database dashboard builder instance.
   */
  public function __construct(
    private DatabaseObservabilitySectionBuilder $databaseObservabilitySectionBuilder,
    private DateFormatterInterface $dateFormatter,
  ) {
  }

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

}
