<?php

declare(strict_types=1);

namespace Drupal\native_observability_execution\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\native_observability_execution\Service\ExecutionTimelineBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\native_observability_execution\Form\ExecutionFiltersForm;
use Symfony\Component\HttpFoundation\Request;

/**
 * Provides the execution monitoring status page.
 *
 * This is the Phase 2 landing page for execution-level insights.
 * For now, it renders a placeholder timeline built from collected spans.
 */
final class ExecutionStatusController extends ControllerBase {

  /**
   * Constructs the controller.
   */
  public function __construct(
    private readonly ExecutionTimelineBuilder $timelineBuilder,
  ) {
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container): self {
    return new self(
      $container->get('native_observability_execution.timeline_builder'),
    );
  }

  /**
   * Page callback.
   *
   * @return array
   *   A render array.
   */
  public function status(Request $request): array {
    $filters_form = $this->formBuilder()->getForm(ExecutionFiltersForm::class);

    return [
      '#type' => 'container',
      'filters' => $filters_form,
      'timeline' => $this->timelineBuilder->buildFromQuery($request->query->all()),
      '#cache' => ['max-age' => 0],
    ];
  }

}
