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

  /**
   * Creates a new overview dashboard builder instance.
   *
   * @param \Drupal\native_observability_dashboard\Service\PrimaryKpiSectionBuilder $primaryKpiSectionBuilder
   *   The primary KPI section builder.
   * @param \Drupal\native_observability_dashboard\Service\RequestTimelineSectionBuilder $requestTimelineSectionBuilder
   *   The request timeline section builder.
   * @param \Drupal\native_observability_dashboard\Service\AjaxActivitySectionBuilder $ajaxActivitySectionBuilder
   *   The AJAX activity section builder.
   * @param \Drupal\native_observability_dashboard\Service\TechnicalInsightsSectionBuilder $technicalInsightsSectionBuilder
   *   The technical insights section builder.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter
   *   The date formatter service.
   */
  public function __construct(
    private PrimaryKpiSectionBuilder $primaryKpiSectionBuilder,
    private RequestTimelineSectionBuilder $requestTimelineSectionBuilder,
    private AjaxActivitySectionBuilder $ajaxActivitySectionBuilder,
    private TechnicalInsightsSectionBuilder $technicalInsightsSectionBuilder,
    private DateFormatterInterface $dateFormatter,
  ) {
  }

  /**
   * Builds the Overview dashboard payload.
   *
   * @param \Drupal\native_observability_dashboard\ValueObject\TimeRange|null $range
   *   The selected dashboard time range.
   *
   * @return array
   *   The complete overview tab payload.
   */
  public function build(?TimeRange $range = NULL): array {
    return [
      'generated_at' => $this->dateFormatter->format(time(), 'custom', 'Y-m-d H:i:s'),
      'sections' => [
        'primary_kpis' => $this->primaryKpiSectionBuilder->build($range),
        'request_timeline' => $this->requestTimelineSectionBuilder->build($range),
        'ajax_activity' => $this->ajaxActivitySectionBuilder->build($range),
        'technical_insights' => $this->technicalInsightsSectionBuilder->build($range),
      ],
    ];
  }

}
